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



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

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。

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

然而,在我的DbContext上,我得到了一个错误,即.WithMany不包含将接受类型集合的输入的WithMany的定义。我知道,当代码第一次创建迁移并更新数据库时,基本上需要构建一个链接表。我认为链接表有两列,没有键。一列为InformationSystemID,一列为ParentInformationSystemID。两者都是外键。我还知道,要使其正常工作,应该限制删除行为,这样,如果链接表中的条目被删除或更新,则更改不会级联(并创建循环(。有人能告诉我正确的方向吗?我需要做什么才能让EF Core 3正确地做到这一点?如果我必须自己创建一个链接表,我该怎么做呢?我需要在DbContext中做什么?我知道链接表会是这样的:
我非常感谢。

public class InfoSysToParentInfoSys
{
public int InfoSysID;
public virtual InformationSystem InfoSys;
public int ParentInfoSysID;
public virtual InformationSystem ParentInfoSys;
}

在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; }
}

最新更新