实体框架核心3.0-创建自引用多对多关系

时间:2020-04-10 05:49:29

标签: c# entity-framework-core many-to-many self-referencing-table

所以这是我的问题...我试图创建的是一种自我参照的多对多关系。基本上这是我的模型。

val four = Succ (Succ (Succ (Succ Zero)))

这个想法是系统可以有很多父母,而一个父母可以有很多孩子。我知道如何创建一个自引用实体,其中许多孩子可以有一个父母。使我绊倒​​的是很多部分。下面是我的DbContext。

public class InformationSystem
{
  public InformationSystem()
  {
     Systems = new HashSet<InformationSystem>();
     ParentSystems = new HashSet<InformationSystem>();
  }
 [Key()]
 public int InformationSystemID { get; set; }
 public string InformationSystemName { get; set; }
 //Navigation properties
 public virtual ICollection<InformationSystem> Systems { get; set; }
 public virtual ICollection<InformationSystem> ParentSystems { get; set; }
}

但是,在我的DbContext上,我收到一个错误:.WithMany不包含许多将接受类型集合输入的定义。我知道,当代码首次创建迁移并更新数据库时,基本上需要构建的是链接表。我认为链接表将具有两列且没有键。一栏为InformationSystemID,一栏为ParentInformationSystemID。两者都是外键。我也知道,要使其正常工作,应该限制删除行为,以便在链接表中删除或更新条目时,该更改不会级联(并创建循环)。有人可以指出正确的方向以使EF Core 3正确执行此操作吗?如果必须自己创建一个链接表,我该怎么做?在DbContext中我需要做什么?我知道链接表看起来像这样:
我将不胜感激。

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  modelBuilder.Entity<InformationSystem>(entity =>
  {
    entity
       .HasMany(e => e.ParentSystems)
       .WithMany(e => e.Systems)
       .OnDelete(DeleteBehavior.Restrict);
  });

1 个答案:

答案 0 :(得分:1)

在EF Core中,必须在模型中包含一个实体以 代表M:N关系中的联接表,然后向其中一个添加导航属性 指向联接实体的多对多关系的另一端。

新表格S:

public class InformationSystem
{
    public InformationSystem()
    {
    }

    [Key()]
    public virtual int InformationSystemID { get; set; }
    public virtual string InformationSystemName { get; set; }

    public virtual ICollection<InformationSystemRelation> Systems { get; set; }
    public virtual ICollection<InformationSystemRelation> ParentSystems { get; set; }

}


public class InformationSystemRelation
{
    public int ParentId { get; set; }
    public InformationSystem Parent { get; set; }

    public int ChildId { get; set; }
    public InformationSystem Child { get; set; }
}

映射:

modelBuilder.Entity<InformationSystemRelation>()
    .HasKey(x => new { x.ParentId, x.ChildId });

modelBuilder.Entity<InformationSystemRelation>()
    .HasOne(x => x.Parent)
    .WithMany(x => x.Systems)
    .HasForeignKey(x => x.ParentId)                    
    .OnDelete(DeleteBehavior.Restrict);


modelBuilder.Entity<InformationSystemRelation>()
    .HasOne(x => x.Child)
    .WithMany(x => x.ParentSystems)
    .HasForeignKey(x => x.ChildId)
    .OnDelete(DeleteBehavior.Restrict);

整个样本:

class Program
{
    static void Main(string[] args)
    {
        var db = new MyDbContext();

        var is1 = new InformationSystem() { InformationSystemName = "is1" };
        var is2 = new InformationSystem() { InformationSystemName = "is2" };
        var is3 = new InformationSystem() { InformationSystemName = "is3" };
        var is4 = new InformationSystem() { InformationSystemName = "is4" };

        db.InformationSystems.Add(is1);
        db.InformationSystems.Add(is2);
        db.InformationSystems.Add(is3);
        db.InformationSystems.Add(is4);

        db.SaveChanges();

        var r1 = new InformationSystemRelation() { ParentId = 1, ChildId = 2 };
        var r2 = new InformationSystemRelation() { ParentId = 1, ChildId = 3 };
        var r3 = new InformationSystemRelation() { ParentId = 4, ChildId = 2 };
        var r4 = new InformationSystemRelation() { ParentId = 2, ChildId = 3 };
        var r5 = new InformationSystemRelation() { ParentId = 2, ChildId = 4 };

        db.InformationSystemRelations.Add(r1);
        db.InformationSystemRelations.Add(r2);
        db.InformationSystemRelations.Add(r3);
        db.InformationSystemRelations.Add(r4);
        db.InformationSystemRelations.Add(r5);

        db.SaveChanges();

        var o2 = db.InformationSystems.Include(x => x.Systems).Include(x => x.ParentSystems).Single(x => x.InformationSystemID == 2);
    }

}

public class InformationSystem
{
    public InformationSystem()
    {
    }

    [Key()]
    public virtual int InformationSystemID { get; set; }
    public virtual string InformationSystemName { get; set; }

    public virtual ICollection<InformationSystemRelation> Systems { get; set; }
    public virtual ICollection<InformationSystemRelation> ParentSystems { get; set; }

}

public class MyDbContext : DbContext
{

    public DbSet<InformationSystem> InformationSystems { get; set; }
    public DbSet<InformationSystemRelation> InformationSystemRelations { get; set; }


    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<InformationSystem>(entity =>
        {
            modelBuilder.Entity<InformationSystemRelation>()
                    .HasKey(x => new { x.ParentId, x.ChildId });

            modelBuilder.Entity<InformationSystemRelation>()
                .HasOne(x => x.Parent)
                .WithMany(x => x.Systems)
                .HasForeignKey(x => x.ParentId)
                .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<InformationSystemRelation>()
                .HasOne(x => x.Child)
                .WithMany(x => x.ParentSystems)
                .HasForeignKey(x => x.ChildId)
                .OnDelete(DeleteBehavior.Restrict);
        });
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("data source=(local)\\SQLEXPRESS;Initial catalog=Test;Integrated security=SSPI");
        base.OnConfiguring(optionsBuilder);
    }
}

public class InformationSystemRelation
{
    public int ParentId { get; set; }
    public InformationSystem Parent { get; set; }

    public int ChildId { get; set; }
    public InformationSystem Child { get; set; }
}
相关问题