EF 多对多的一种方式将相同的项目两次添加到 child collection 不会被保存
EF many to many one way add same item twice to child collection doesn't get saved
感谢您花时间阅读本文。一个星期以来,我一直在寻找这个问题的答案,但我 运行 没有想法...
这是场景:
型号:
public class Parent{
public Guid Id {get;set;}
public virtual ICollection<Child> ChildCollection{ get; set; }
}
public class Child {
public Guid Id {get;set;}
public string Name{get;set}
}
创建模型时:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Parent>()
.HasMany(c => c.ChildCollection)
.WithMany()
.Map(x =>
{
x.ToTable("ParentToChildMapping");
x.MapLeftKey("ParentId");
x.MapRightKey("ChildId");
});
}
现在的问题:
如果我在 collection 中有一个 Parent object 和一个 Child object 并尝试添加一个新的 object与 child collection 相同的 ID DbContext 不会 "detect" 更改并且不会在映射 table 中添加新条目。
但是,如果我添加一个新的 Child(collection 中不存在),它确实可以添加。
例如:
{
var childId = Guid.Parse("cbd5bccc-b977-4861-870d-089994958cdf");
var parent = new Parent { ChildCollection = new HashSet<Child>() };
var context = new DBContext();
var child = context.Childs.Single(c=>c.Id=childId);
parent.ChildCollection.Add(child);
context.Parents.Add(parent);
context.SaveChanges();
}
然后:
{
var childId = Guid.Parse("cbd5bccc-b977-4861-870d-089994958cdf");
var context = new DBContext();
var parent = dbContext.Parents.Include(p=>p.ChildCollection).Single(p=>p.Id=parentId); // The id saved in the point before.
var child = context.Childs.Single(c=>c.Id=childId);
parent.ChildCollection.Add(child);
// At this point parent.ChildCollection.Count() = 2
context.SaveChanges();
}
collection的计数是2,没问题。但保存更改不会添加该项目。如果我再次使用上下文从数据库中检索它,它只会 collection.
中的 returns 1 个元素
任何想法都欢迎。
您正试图将相同的 child 添加到 parent 两次 ,但这是行不通的。如果你成功了,你最终会在数据库中得到如下内容:
- 一个Child, ID = "cbd5bccc-b977-4861-870d-089994958cdf"
- 一个Parent, ID = "the-parent-id"
- 两个 ParentToChild映射,都有ParentId = "the-parent-id"和ChildId = "cbd5bccc-b977-4861-870d-089994958cdf"
这是关系型数据库不允许的,因为(ParentId,ChildId)是ParentToChild映射的主键,而两个table 中的行不能有相同的主键。
感谢您花时间阅读本文。一个星期以来,我一直在寻找这个问题的答案,但我 运行 没有想法... 这是场景:
型号:
public class Parent{
public Guid Id {get;set;}
public virtual ICollection<Child> ChildCollection{ get; set; }
}
public class Child {
public Guid Id {get;set;}
public string Name{get;set}
}
创建模型时:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Parent>()
.HasMany(c => c.ChildCollection)
.WithMany()
.Map(x =>
{
x.ToTable("ParentToChildMapping");
x.MapLeftKey("ParentId");
x.MapRightKey("ChildId");
});
}
现在的问题:
如果我在 collection 中有一个 Parent object 和一个 Child object 并尝试添加一个新的 object与 child collection 相同的 ID DbContext 不会 "detect" 更改并且不会在映射 table 中添加新条目。 但是,如果我添加一个新的 Child(collection 中不存在),它确实可以添加。
例如:
{
var childId = Guid.Parse("cbd5bccc-b977-4861-870d-089994958cdf");
var parent = new Parent { ChildCollection = new HashSet<Child>() };
var context = new DBContext();
var child = context.Childs.Single(c=>c.Id=childId);
parent.ChildCollection.Add(child);
context.Parents.Add(parent);
context.SaveChanges();
}
然后:
{
var childId = Guid.Parse("cbd5bccc-b977-4861-870d-089994958cdf");
var context = new DBContext();
var parent = dbContext.Parents.Include(p=>p.ChildCollection).Single(p=>p.Id=parentId); // The id saved in the point before.
var child = context.Childs.Single(c=>c.Id=childId);
parent.ChildCollection.Add(child);
// At this point parent.ChildCollection.Count() = 2
context.SaveChanges();
}
collection的计数是2,没问题。但保存更改不会添加该项目。如果我再次使用上下文从数据库中检索它,它只会 collection.
中的 returns 1 个元素任何想法都欢迎。
您正试图将相同的 child 添加到 parent 两次 ,但这是行不通的。如果你成功了,你最终会在数据库中得到如下内容:
- 一个Child, ID = "cbd5bccc-b977-4861-870d-089994958cdf"
- 一个Parent, ID = "the-parent-id"
- 两个 ParentToChild映射,都有ParentId = "the-parent-id"和ChildId = "cbd5bccc-b977-4861-870d-089994958cdf"
这是关系型数据库不允许的,因为(ParentId,ChildId)是ParentToChild映射的主键,而两个table 中的行不能有相同的主键。