开发者

Regarding "Conflicting changes to the role" Exception

Actually I am getting an exception

Conflicting changes to the role 'TableName' of the relationship 'DataModel.FK_TableName_RelateTableName' detected

when ApplyChanges method is called from the ObjectContext. I dont have any idea what this exception is all about. I just wanted to know the cause of开发者_高级运维 this exception.


I think you get these sorts of exceptions if you are trying to get the foreign key relationship for the same entity twice. A simple example would be this:

var tn = ctx.TableName.Where(t => t.TableNameId == 1).First();
var rel = new RelateTableName {TableName = tn, TableNameId = 2};
ctx.RelateTableName.AddObject(rel);

Here you can see that I set the TableNameId property to 2 but the TableName property to an object with the TableNameId of 1 - so the entity framework cannot work out which one is the correct relationship to put into the database.


The actual reason behind this issue is, when we go for any SaveChanges the context needs to be dispatched properly in order to proceed with the next SaveChanges to insert another record into DB on the same item with a different foreign key. You just need to add the below line after "context.SaveChanges()"

context.Entry(your object).State = System.Data.Entity.EntityState.Detached;

This will solve the conflicts. Multiple insertion with same context results in conflicts.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜