EF4.3 多对多 table 作为实体

EF4.3 many to many table as entity

我有两个实体,两个是正常的,另一个是彼此之间的映射,每当我打开导航映射时,我都会得到无效的对象名称。 如果我尝试映射,我会得到 table 已经在架构中。

EntityA EntityB        EntityC
Id      Id             EntityA has FK} PrimaryKey 
Name    description    EntityB has FK}

映射类 在尝试之后,我已经在 EntityB 上进行了导致我的问题和实体的所有导航

我在实体 c 中尝试了此 ID,但仍然得到无效的 table 名称。它试图创建的名称是 dbo.BA,而实际的 table 是 dbo.tblAB

 //entity.HasRequired(p => p.A).WithMany(p =>   p.C).HasForeignKey(p => p.EntityA);
 //entity.HasRequired(p => p.B).WithMany(p => p.C).HasForeignKey(p => p.EntityB)

我无法使用 .map,因为我需要在 DAL 中使用所有三个实体 映射 类 实体C

entity.HasKey(t => new {t.EntityA, t.EntityB});
            entity.Property(qa => qa.EntityA).IsRequired();
            entity.Property(qa => qa.EntityB.).IsRequired();
            entity.toTable("tblC)

这正在退出 table,因为我们需要删除这些记录,所以我们需要通过实体来完成。 实体 B 和实体 A 具有实体 C 的 ICollection 属性和

的映射

在实体 A 映射中

entity.HasMany(g => g.C).WithRequired().HasForeignKey(p => p.EntityA).WillCascadeOnDelete(false);

在实体 B 映射中

entity.HasMany(g => g.C).WithRequired().HasForeignKey(p => p.EntityB).WillCascadeOnDelete(false);

在实体 C 映射中

entity.Property(t => t.EntityA).HasColumnName("EntityA");
            entity.Property(t => t.EntityB).HasColumnName("EntityB");
            entity.Map(c => c.ToTable("tblC")).HasKey(t => new { t.EntityA, t.EntityB });*

我收到多重性错误,但现在收到无效的列名 'EntityC_EntityA'。列名称无效 'EntityC_EntityB'。


更新 我现在已经修复了架构 在实体 C

[Key, Column(Order = 0), ForeignKey("EntityA")]
[Key, Column(Order = 1), ForeignKey("EntityB"))

导航属性

[ForeignKey("Id")]
public virtual EntityA EntityA { get; set; }
[ForeignKey("Id")]
public virtual EntityB EntityB { get; set; }

在实体A和b映射中

    entity.HasMany(g => g.Cs).WithRequired(p=>p.EntityA).HasForeignKey(p => p.EntityA).WillCascadeOnDelete(false);
entity.HasMany(g => g.Cs).WithRequired(p=>p.EntityB).HasForeignKey(p => p.EntityB).WillCascadeOnDelete(false);

问题知道查询正在尝试创建 db.BA table。 我试过的查询是

ctx.EntityAs.Include(p => p.Cs.Select(pr => pr.EntityB))
        .Include(p => p.EntityBs.Select(pr => pr.Cs.Select(ps =>    ps.EntityA)))

感谢 Gert Arnold 提出的写作方向。模式的答案在问题中 解决许多问题的 Linq 是在 B 或 A

上包含带有 select 的 EntityC
entityA.Include(p => p.Cs.Select(pr => pr.EntityA)) 

entityB.Include(p => p.Cs.Select(pr => pr.EntityB))