Referential integrity violation occurred

I am trying to update an existing object.

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 Attach methods throw an exception:

A violation of the referential integrity constraint has occurred. Property values ​​that define referential constraints are incompatible between the main and dependent objects in the relation.

How can i fix this?

+7
source share
6 answers

What is the definition of an item object? It seems that in some of its collections that establish reality with other entities, there is some kind of conflict. You can try to clear all collections to make sure that the problem persists, but in this case you have lost the assignment of a foreign key. But perhaps this will help you find the problem.

This could be advice. When I try to snap an existing object to a context, I use the following:

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

You can add using System.Data to avoid having to write all this time.

Attach the object in the desired state, changing it in this case and tracking the changes. This is one line instead of two.

+4
source

It looks like you have a relationship with a foreign key field and a navigation property in item , and these fields have conflicting values. This happens when loading an object and related objects, changing the relationship at one end, mark only this end as Modified and try to save. Make sure you change the relationship at both ends and mark all affected objects as Modified before calling SaveChanges .

+8
source

I came across this exception under various circumstances and am posting here since this question arises when searching for an error message.

An exception was IObjectContextAdapter.ObjectContext.AttachTo(entitySetName, entity) when IObjectContextAdapter.ObjectContext.AttachTo(entitySetName, entity) was called with a partially loaded object. Foreign keys on the object were defined, but navigation properties were not loaded. (That is, O.ItemID matters, but O.Item is null). Specific circumstances prevented O.Item .

The problem was that the state manager of the object loaded the object into a separate method and was already tracking the object defined using the same keys. Since a separate method did not require tracking the state of the object, the problem was solved by calling IQueryable.AsNoTracking() inside this method.

+4
source

The problem for me was that the entity infrastructure loaded my object in several places, so when I updated the foreign key, there were now two links to the same object, one with a foreign key, pointing to record a and one with foreign key pointing to record b, which caused an error, since my relationship is single-handed. To solve this problem, I used context.Entry (Object) .State = EntityState.Detached, reloaded the object, changed the foreign key and then saved my changes.

+3
source

Let's say you have the following scheme: schema

If you want to edit CurrentLocationId in Person, you also need to edit the CurrentLocation object embedded in the Person object. EF will automatically populate the CurrentLocation object, because CurrentLocationId has a foreign key in the CurrentLocation table. When you edit CurrentLocationId without updating also the CurrentLocation object, they become out of sync. This is what throws an exception in this case.

Suppose you need to update the Person object CurrentLocationId. We assume that you have preloaded Person data and Location data.

 public class DbData { List<Person> PersonList; List<Location> LocationList; public DbData() { using (var context = new MyContext()) { PersonList = context.Persons.ToList(); LocationList = context.Locations.ToList(); } } public void UpdatePersonLocation(Person person, int newLocationId) { using (var context = new MyContext()) { var location = LocationList.Where(l=>l.id==newLocationId).Single(); //you need to update both the id and the location for this to not throw the exception person.CurrentLocationId == newLocationId; person.CurrentLocation == location; context.Entry(person).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } } //or if you're giving it the location object... public void UpdatePersonLocation(Person person, Location location) { using (var context = new MyContext()) { //you need to update both the id and the location for this to not throw the exception person.CurrentLocationId == location.id; person.CurrentLocation == location; context.Entry(person).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } } } 
0
source

It may be an old post, but the following worked for me

set the SaveOptions option SaveOptions.DetectChangesBeforeSave to SaveOptions.DetectChangesBeforeSave

0
source

All Articles