Home » Ado.Net » Entity Framework » Conflicting changes to the role X of the relationship Y have been detected.

Conflicting changes to the role X of the relationship Y have been detected.

Entity Framework can be something hard to debug. This error was known by me when people use reference to an object and also an integer for the ID within the same object and change both of them. When this happen, Entity Framework raise an error because it cannot know which one is the correct reference.

public class MyEntity
{
   public virtual MySecondEntity MySecond{get;set;}
   public int MySecondEntityID{get;set;}
}

The code above can trig the error :

Conflicting changes to the role X of the relationship Y have been detected.

To solve this problem, you only need to change the scalar property (integer) and not touching the complex attribute (the property).

But, let say that you have this error without having this kind of structure. Let say you only use reference to the object (like I do).

public class MyEntity
{
   public virtual MySecondEntity MySecond{get;set;}
}

How can I have this error? Well, I had this problem when MyEntity was brand new (ID = 0) and when MySecond also a reference to MyEntity. This is because MySecondEntity has a reference to MyEntity which is ID = 0. Those 2 MyEntity will be treated as 2 differents entity by Entity Framework. The trick to solve this issue is to set to NULL the reference inside MySecondEntity and let Entity Framework set in the database the reference. This can be done because the new MyEntity does have a reference to the MySecondEntity property.

To conclude, if you have a conflicting change and you are using code first with property reference, be sure that when you insert a new entity that not both reference to an entity with a new ID. Just one part of the navigation require to be defined.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

4 Responses so far.

  1. Keyse says:

    Setting to NULL the reference inside MySecondEntity WORKS however, I still have the feeling that its not an elegant solution.

    I mean we went through the trouble of correctly defining the structure of our Domain Entities/Models ONLY to set some of the references back to null just before calling EF so to make EF happy!!!

    Do you know of any extension methods to DbContext that detects this problem.?

    • I do not know any extension that detect this problem. However, if you are using Model that is not the context entity, than in the mapping you could automatically set the null. Personally, setting parent entity to their children and not setting child to parent (so by default NULL) has always been an option which is efficient.

  2. Dirk says:

    Great! I am incredibly happy to have found your page. I removed all set-complex-Attribute callers and the error was gone.
    Thank you very much!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.