Chr*_*cki 3 c# many-to-many self-referencing-table entity-framework-core
所以这是我的问题......我试图创建的是一种自我引用的多对多关系。基本上这是我的模型。
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; }
}
Run Code Online (Sandbox Code Playgroud)
这个想法是,一个系统可以有许多父母,而一个父母可以有许多孩子。我知道如何创建一个自引用实体,其中许多孩子可以有一个父母。让我困惑的是多对多的部分。下面是我的 DbContext。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<InformationSystem>(entity =>
{
entity
.HasMany(e => e.ParentSystems)
.WithMany(e => e.Systems)
.OnDelete(DeleteBehavior.Restrict);
});
Run Code Online (Sandbox Code Playgroud)
然而,在我的 DbContext 上,我收到一个错误,即 .WithMany 不包含 with Many 的定义,该定义将接受类型集合的输入。我知道,当代码首先创建迁移并更新数据库时,基本上需要构建的是链接表。我认为链接表有两列,没有键。一列是 InformationSystemID,一列是 ParentInformationSystemID。两者都是外键。我还知道,要使其正常工作,应限制删除行为,以便在链接表中删除或更新条目时,该更改不会级联(并创建循环)。有人可以指出我需要做什么才能让 EF Core 3 正确执行此操作的正确方向吗?如果我必须自己创建一个链接表,我该怎么做?我需要在 DbContext 中做什么?我知道链接表看起来像这样:
我非常感激。
public class InfoSysToParentInfoSys
{
public int InfoSysID;
public virtual InformationSystem InfoSys;
public int ParentInfoSysID;
public virtual InformationSystem ParentInfoSys;
}
Run Code Online (Sandbox Code Playgroud)
在 EF Core 中,需要在模型中包含一个实体来表示 M:N 关系中的联接表,然后将导航属性添加到指向联接实体的多对多关系的任一侧。
新表:
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; }
}
Run Code Online (Sandbox Code Playgroud)
映射:
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);
Run Code Online (Sandbox Code Playgroud)
整个样本:
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; }
}
Run Code Online (Sandbox Code Playgroud)