7

我正在尝试更新现有实体。

我有以下代码:

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

但是这些Attach方法会引发异常:

发生参照完整性约束冲突:定义参照约束的属性值在关系中的主体对象和从属对象之间不一致。

我怎样才能解决这个问题?

4

6 回答 6

8

似乎您与 中的外键字段和导航属性有某种关系item,并且这些字段具有冲突的值。当您加载实体及其相关实体、更改一端的关系、仅将该端标记为Modified并尝试保存时,就会发生这种情况。确保在两端修改关系并像Modified调用之前一样标记所有受影响的实体SaveChanges

于 2013-04-21T07:12:21.040 回答
5

我在不同的情况下遇到了这个异常,并在这里发布,因为在搜索错误消息时会出现这个问题。

IObjectContextAdapter.ObjectContext.AttachTo(entitySetName, entity)使用部分加载的实体调用时引发异常。定义了实体上的外键,但未加载导航属性。(也就是说,O.ItemID有一个值,但O.Item为空)。具体情况不允许O.Item加载。

问题原来是对象状态管理器已将对象加载到单独的方法中,并且已经在跟踪使用相同键定义的对象。由于单独的方法不需要跟踪对象状态,因此通过IQueryable.AsNoTracking()在该方法中调用来解决问题。

于 2015-03-28T21:38:04.570 回答
4

item 对象的定义是什么?似乎在其与其他实体建立关系的某些集合中存在某种类型的冲突。您可以尝试清除所有集合以查看问题是否仍然存在,但在这种情况下您丢失了外键分配。但也许它可以帮助您找到问题所在。

这可能是一个提示。当我尝试将现有实体附加到上下文时,我会执行以下操作:

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

您可以添加 System.Data 的使用以避免一直需要编写它。

这会将实体附加到您想要的状态,在这种情况下进行修改并跟踪更改。这是一行而不是两行。

于 2013-04-21T09:16:49.877 回答
3

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

于 2015-07-21T16:14:47.943 回答
0

Lets say you have the following schema: schema

If you want to edit the 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's table. When you edit the CurrentLocationId without updating the CurrentLocation object as well, they become out of sync. This is what causes the exception in this case.

So let's say you needed to update the Person object's CurrentLocationId. We'll assume you pre-fetched the Person data and the 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();
        }
    }
}
于 2018-11-07T18:07:00.777 回答
0

This might be an old post but the following worked for me

set the SaveOptions option to SaveOptions.DetectChangesBeforeSave

于 2019-05-25T20:34:00.587 回答