A referential integrity constraint violation on a many-to-many connection in Entity Framework

c# entity-framework

Question

Hello, I have a program that makes extensive use of entity framework and inproc caching. I reconnect the cached copy each time I wish to write an update to an object. To avoid trying to attach the same item repeatedly, I keep track of everything I've attached during the context's life cycle.

Very seldom, this error occurs during attach and states the following and happens very quickly:

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.

I have carefully examined the entity, which seems to be normal. I believe that the attachment/detachment of a foreign key while fixups executes is the cause of this problem.

Can this mistake occur for causes other than the object being in a state that EF wasn't anticipating, or is there a decent method to find out more about it?

EDIT: DB Diagram (note that I'm using CodeFirst; I simply made the diagram using the EDMX tool; I also removed a lot of the model's usual characteristics for simplicity).

enter image description here

1
35
4/3/2012 9:25:01 PM

Accepted Answer

A one-to-many connection between two parties might lead to a mistake.Person and Location In addition to the many-to-many connection, you also seem to have. For instance, the code below would raise the exception:

using (var context = new MyContext())
{
    var person = new Person
    {
        CurrentLocationId = 1,
        CurrentLocation = new Location { Id = 2 }
    };
    context.People.Attach(person); // Exception
}

The foreign key property value is "the property values that define the referential restrictions."CurrentLocationId as well as the main important valueCurrentLocation.Id If such values vary, an exception is raised. (HavingCurrentLocation as null though is ok.)

In my perspective, foreign key associations are the only ones for which this error may be raised since they are the only ones for which your model has any attributes that establish referential restrictions. For autonomous organizations, it cannot be thrown. Since there is no foreign key property in the model and each many-to-many connection is a distinct association, my best bet is that the mistake is with the one-to-many relationship rather than your many-to-many relationship.

50
7/21/2012 10:45:06 PM

Popular Answer

I discovered a pretty similar exception:

"A referential integrity constraint violation occurred: 
The property value(s) of 'ObjectA.PropertyX' on one end of a relationship 
do not match the property value(s) of 'ObjectB.PropertyY' on the other end."

The cause was that the navigation property was completely provided by the client when the client side of the web API issued a PUT request with the whole object, in this case ObjectA (or more precisely ObjectB.ObjectA), which was a navigation property. This happens as a result of the client receiving the complete object from the server and sending it back unchanged to the server.

However, the ObjectB.PropertyY had recently been modified (this was the reason for the PUT request in the first place).

EF attempted to reconcile this and failed with the previously mentioned error because ObjectB.PropertyY had a reference to the same object ObjectA (a foreign key).

The answer was easy:

ObjectB.ObjectA = null;

before the SaveChanges() entirely fixed this.

Hope someone finds this useful.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow