Conflicting changes to the role x of the relationship y have been detected

馋奶兔 提交于 2019-11-30 02:47:24

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.

So, MyEntity contains a list of Group which contain a reference back to MyEntity.

The problem is that MyEntity.Group.MyEntity seems to be "new and not the same" as MyEntity. When adding MyEntity to the context, it found a conflict.

To solve the problem I set Group.MyEntity to NULL and keep the reference of Group inside the list of MyEntity. When saving, MyEntity reference (ID) is set into the Group table.

I am not sure it's the cleanest way to do it, but the problem is finally solved.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!