none
ChangeTracker.DetectChanges() : a referential integrity constraint violation occurred RRS feed

  • Question

  • A referential integrity constraint violation occurred: A primary key property that is a part of referential integrity constraint cannot be changed when the dependent object is Unchanged unless it is being set to the association's principal object. The principal object must be tracked and not marked for deletion.

    This error message occur into one of our system. The system is kind of huge and unfortunately, I cannot provide sample of code. Mainly because it has way a lot of entities, layers of data access etc.

    The message does not provide which entity the integrity is violated, neither which configuration is wrong (if this is the cause).

    The only thing I know is that if I do not call the ChangeTracker.DetectChanges() than I do not have any exception. Also note that I have that configuration: 

                Configuration.AutoDetectChangesEnabled = false;
                Configuration.LazyLoadingEnabled = false;
                Configuration.ProxyCreationEnabled = false;

    I was able to create a unit test that reproduce the same error. However, the code is huge to paste here and won't be of help since it requires many classes and more than 8 entities. However I know that it crashes when I attach an existing entity that come from DbSet<MyEntity>().Find(id)

    As mentionned, if I do not do the DetectChanges(), everything work fines, the entity is attached correctly and the database FK are all well done.

    I must keep the DetectChanges because a lot of other logic pass through that saving method where the DetectChange's method is present.

    My question is about, how with Entity Framework can I easily debug this kind of message? And home come the DetectChanges cause problem when I am getting that entity from Entity Framework if this entity's state is set to Unchanged.

    Saturday, November 15, 2014 5:33 AM

Answers

  • I am not updating the primary key. The schema is way to complicated to publish here. Multiples tables and multiples foreign keys which make it a big relational tree of tables. And, I am not able to produce a small snippet to simulate the error.

    So far, the hack that I did was to kill the DbContext and create a new one between the two operations and it worked. This is ugly, but at some point, we do not want to waste more men's hours on that problem. People are using an ORM to be quick and issue like that, without real debug information, make it very hard to fix. I guess this is why most example on the web use small schema with small operations during the life of the DbContext.

    Calling Detecting change after a Find should not do a difference at any point to violate a constraint. Weird bug...

    Thank you for your time, I do not think I will be able to give you anymore detail and since Entity Framework is very limited in its error message, this hack will stay there for a long time I suppose... sad.

    Monday, November 24, 2014 6:34 PM

All replies

  • The only way to make it works so far is to re-instantiate the DbContext with a new one which should not be the case. I added some output in the console and I see from the Change Tracker that my 2 new entities are with the Insert state while the one from the Find() method are Attached. That looks fine to me.

    However, I cannot figure out what produce the error because Entity is not enough specify in its error message.

    Saturday, November 15, 2014 6:25 PM
  • Hello Patrick,

    >> However I know that it crashes when I attach an existing entity that come from DbSet<MyEntity>().Find(id)

    As far as I know, the Find method would call the DetectChanges method internally.

    >> My question is about, how with Entity Framework can I easily debug this kind of message?

    Since we do know the exact table schema, we are hard to reproduce this issue, here is a some similar thread discussing this issue might be helpful:

    https://social.msdn.microsoft.com/Forums/en-US/12fd7e56-2f4d-49e2-ab27-7cfcca6b3e17/entity-framework-updating-composite-primary-key?forum=adodotnetentityframework

    This one is under a many to many relationship, and the op wants to update the primary key however, this is not allowed, and the solution is to delete one row and insert a new one instead.

    Please check if you have similar tables and do the same operation in your system.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, November 17, 2014 2:31 AM
    Moderator
  • I am not updating the primary key. The schema is way to complicated to publish here. Multiples tables and multiples foreign keys which make it a big relational tree of tables. And, I am not able to produce a small snippet to simulate the error.

    So far, the hack that I did was to kill the DbContext and create a new one between the two operations and it worked. This is ugly, but at some point, we do not want to waste more men's hours on that problem. People are using an ORM to be quick and issue like that, without real debug information, make it very hard to fix. I guess this is why most example on the web use small schema with small operations during the life of the DbContext.

    Calling Detecting change after a Find should not do a difference at any point to violate a constraint. Weird bug...

    Thank you for your time, I do not think I will be able to give you anymore detail and since Entity Framework is very limited in its error message, this hack will stay there for a long time I suppose... sad.

    Monday, November 24, 2014 6:34 PM
  • Hello Patrick,

    After searching more, I saw some same discussions, and the solution was that you used currently, to create a new context class.

    Anyway, it glad to hear that there is a workaround for this issue although it might be ugly.

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 25, 2014 8:38 AM
    Moderator