简体   繁体   English

无法使用POCO更新Entity Framework 4中的实体

[英]Cannot update entity in Entity Framework 4 using POCO

I have 2 tables : Item and Location (one - many ). 我有2个表:项目和位置(一个或多个)。

I select one location and I try to update it 我选择一个位置,然后尝试更新它

The entity goes from {Id=2, Name="name1",City="city1",Items=null} to {Id=2, Name="name1", City="city2",Items=null} and i want to save the updates. 实体从{Id=2, Name="name1",City="city1",Items=null}变为{Id=2, Name="name1", City="city2",Items=null} ,我想要保存更新。 The update method from the base class is: 基类的更新方法是:

        public virtual void Update(T entity)
        {
            Entities.Attach(entity);
            Context.ObjectStateManager
                   .ChangeObjectState(entity, System.Data.EntityState.Modified);
        }

I make several updates just like this on other tables with no problems but in this case I get "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key." 我像在其他表上一样进行了几次更新,都没有问题,但是在这种情况下,我得到"An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key." and I really don't understand why. 我真的不明白为什么。

You can try specifying: MergeOptions.NoTracking for the context and see if that solves your problem. 您可以尝试为上下文指定: MergeOptions.NoTracking ,看看是否可以解决您的问题。

Also, you can try Context.ObjectStateManager.TryGetObjectStateEntry(entity, out stateEntry) to see if the object is there and can be updated. 另外,您可以尝试使用Context.ObjectStateManager.TryGetObjectStateEntry(entity, out stateEntry)来查看对象是否存在并且可以更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM