与EF4.3 Code First相同的表与多对多关系

时间:2012-05-18 19:17:58

标签: entity-framework entity-framework-4.1 ef-code-first entity-framework-ctp5

如何使用此架构进行配置?

CREATE TABLE Entity
(
    Id int identity primary key,
    Name nvarchar(30)
)

CREATE TABLE Member
(
    ParentEntityId references Entity(Id),
    ChildEntityId references Entity(Id)
)

1 个答案:

答案 0 :(得分:2)

像这样:

模特课:

public class Entity
{
    public int Id { get; set; }
    public string Name { get; set; }

    public ICollection<Entity> Parents { get; set; }
    public ICollection<Entity> Children { get; set; }
}

映射:

modelBuilder.Entity<Entity>()
    .HasMany(e => e.Parents)
    .WithMany(e => e.Children)
    .Map(m =>
    {
        m.ToTable("Member");
        m.MapLeftKey("ParentEntityId");
        m.MapRightKey("ChildEntityId");
    });