Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A referential integrity constraint violation occurred

I'm trying to update an existing entity.

I have the following code:

public MamConfiguration_V1 Save(MamConfiguration_V1 item)
{
    mMaMDBEntities.MamConfiguration_V1.Attach(item);
    mMaMDBEntities.ObjectStateManager.ChangeObjectState(item, System.Data.EntityState.Modified);
    mMaMDBEntities.SaveChanges();
    return item;
}

But the Attach methods throws an exception:

A referential integrity constraint violation occurred: The property values that define the referential constraints are not consistent between principal and dependent objects in the relationship.

How can I fix this?

like image 257
Elad Benda Avatar asked Apr 21 '13 06:04

Elad Benda


4 Answers

Seems like you have some relationship with foreign key field and a navigation property in the item, and those fields have conflicting values. This occurs when you load an entity and its related entities, change the relationship at one end, mark only that end as Modified and attempt to save. Make sure you modify relationship at both ends and mark all the affected entities as Modified before calling SaveChanges.

like image 177
Mat J Avatar answered Nov 15 '22 23:11

Mat J


I encountered this exception under a different set of circumstances, and am posting here since this question comes up when the error message is searched.

The exception was thrown when calling IObjectContextAdapter.ObjectContext.AttachTo(entitySetName, entity) with a partially-loaded entity. The foreign keys on the entity were defined, but the navigational properties were not loaded. (That is, O.ItemID had a value, but O.Item was null). The specific circumstances did not allow O.Item to be loaded.

The problem turned out to be that the Object State Manager had loaded the object in a separate method and was already tracking the object defined with the same keys. Since the separate method did not need to track the object state, the issue was resolved by calling IQueryable.AsNoTracking() within that method.

like image 43
drf Avatar answered Nov 16 '22 00:11

drf


What is the definition of the item object? It seems that in some of its collections that set the realionship with other entities exist some type of conflict. You could try to clear all the collections to see if the problem persists, but in this case you lost the foreign key assignment. But perhaps it could help you to locate the problem.

This could be a tip. When I try to attach an existing entity to the context, I use to do the following:

mMaMDBEntities.Entry<MamConfiguration>(item).State = System.Data.EntityState.Modified;

You can add the using of System.Data to avoid the needed to write it all the time.

This attach the entity in the state that you want, modified in this case and track the changes. This is one line instead of two.

like image 30
Álvaro García Avatar answered Nov 15 '22 23:11

Álvaro García


The issue for me was that entity framework had loaded my object in multiple places, so when I updated a foreign key, there were now two references to the same object, one with a foreign key pointing to record a and one with a foreign key pointing to record b, which caused an error since my relationship is one to one. To resolve it, I used context.Entry(Object).State = EntityState.Detached, reloaded the object, made the foreign key change and then saved my changes

like image 36
Snicklefritz Avatar answered Nov 15 '22 23:11

Snicklefritz