C# 已检测到关系 y 的角色 x 的冲突更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11939964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Conflicting changes to the role x of the relationship y have been detected
提问by Patrick Desjardins
I am having the exception
我有例外
Conflicting changes to the role x of the relationship y have been detected.
已检测到关系 y 的角色 x 的冲突更改。
Every time I add my entity to my context
每次我将我的实体添加到我的上下文时
Database.MyEntitys.Add(MyEntity);
The class MyEntity contain this property :
MyEntity 类包含此属性:
public virtual ICollection<DetailInfo> Group { get; set; }
The DetailInfo class is pretty simple:
DetailInfo 类非常简单:
public class DetailInfo:BaseEntity {
public virtual Detail Detail { get; set; }
public decimal Total { get; set; }
public virtual MyEntity MyEntity { get; set; }
}
The DatabaseContext is also simple:
DatabaseContext 也很简单:
public class MyEntityConfiguration : EntityTypeConfiguration<MyEntity> {
public MyEntityConfiguration() {
HasMany(e => e.Group).WithRequired(s => s.MyEntity).WillCascadeOnDelete(true);
}
}
public class DetailInfoConfiguration : EntityTypeConfiguration<DetailInfo> {
public DetailInfoConfiguration() {
HasRequired(x => x.MyEntity).WithMany(s => s.Group);
HasRequired(x => x.Detail);
HasKey(s => s.ID);
ToTable("DetailInfo");
}
}
On the Database side the table MyEntity has a primary key to the column ID. The DetailInfo has also a primary key called ID. DetailInfo contains 2 FK, one to MyEntity and one to Detail which is another entity.
在数据库端,表 MyEntity 有一个列 ID 的主键。DetailInfo 还有一个名为 ID 的主键。DetailInfo 包含 2 个 FK,一个到 MyEntity,一个到 Detail,后者是另一个实体。
In the problematic scenario, the MyEntity is new has a new Detail. I am expecting a new entry for MyEntity with a new Detail and having all FK correctly setup.
在有问题的场景中,MyEntity is new 有一个新的 Detail。我期待 MyEntity 的新条目具有新的 Detail 并正确设置所有 FK。
Edit:
编辑:
here is the Insert:
这是插入:
public virtual int Insert(MyEntity myEntity) {
if (myEntity.Group != null && myEntity.Group.Count() == 0) {
myEntity.Group = null;
}
if (myEntity.Group != null) {
foreach (var g in myEntity.Group)
{
if (g.PropertyOneToOne != null) {
if (g.PropertyOneToOne.ID == 0) {
myEntity.PropertyOneToOne = null;
}
else {
if (!Database.PropertyOneToOnes.Local.Any(e => e.ID == g.PropertyOneToOne.ID)) {
Database.PropertyOneToOnes.Attach(g.PropertyOneToOne);
}
myEntity.PropertyOneToOne = Database.PropertyOneToOnes.Local.Single(e => e.ID == g.PropertyOneToOne.ID);
}
}
else {
myEntity.PropertyOneToOne = null;
}
}
}
Database.MyEntitys.Add(myEntity);
}
采纳答案by Patrick Desjardins
The problem is this one:
问题是这个:
MyEntity has an ID of 0 since it's a new MyEntity. The Group is also new and contain a reference to MyEntity.
MyEntity 的 ID 为 0,因为它是一个新的 MyEntity。该组也是新的并包含对 MyEntity 的引用。
So, MyEntitycontains a list of Group which contain a reference back to MyEntity.
因此,MyEntity包含一个 Group 列表,其中包含对MyEntity.
The problem is that MyEntity.Group.MyEntityseems to be "new and not the same" as MyEntity. When adding MyEntity to the context, it found a conflict.
问题是它MyEntity.Group.MyEntity似乎与MyEntity. 将 MyEntity 添加到上下文时,发现冲突。
To solve the problem I set Group.MyEntityto NULL and keep the reference of Group inside the list of MyEntity. When saving, MyEntityreference (ID) is set into the Group table.
为了解决这个问题,我设置Group.MyEntity为 NULL 并将 Group 的引用保留在MyEntity. 保存时,MyEntity引用(ID)被设置到组表中。
I am not sure it's the cleanest way to do it, but the problem is finally solved.
我不确定这是最干净的方法,但问题终于解决了。
回答by Mochamad Fazar
Some other solution might be we need to configure 2 way binding data because MyEntity need Group and Group has MyEntity. You can refer to this link to configure two way binding. Hope it helps. EF 5.0 Code First Two way navigation withought foreign key id in child
其他一些解决方案可能是我们需要配置 2 路绑定数据,因为 MyEntity 需要 Group 并且 Group 有 MyEntity。您可以参考此链接配置双向绑定。希望能帮助到你。EF 5.0 Code First 双向导航,在孩子中没有外键ID
回答by Arvand
The problem could also be that you are creating a new child list and you are assigning that list to multiple items of the parent list, the solution is that you should create a new child list for each of the parent items.
问题也可能是您正在创建一个新的子列表,并且您将该列表分配给父列表的多个项目,解决方案是您应该为每个父项目创建一个新的子列表。

