"具有相同键的对象已经存在于ObjectStateManager ..."设置实体状态时,修改会抛出异常 [英] "An object with the same key already exists in the ObjectStateManager..." exception is thrown when setting an entity state to modified

查看:104
本文介绍了"具有相同键的对象已经存在于ObjectStateManager ..."设置实体状态时,修改会抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟一些例子(包括这样的书是临ASP.NET MVC 3和专业的ASP.NET MVC 3)创建简单的ASP.NET MVC使用EF 4.1 3应用程序(因为我是新来的这些技术)。

I followed some examples(including such books as "Pro ASP.NET MVC 3" and "Professional ASP.NET MVC 3") to create simple ASP.NET MVC 3 apps using EF 4.1 (since I'm new to these technologies).

我用下面的库(它的单个实例所使用的控制器的所有行为方法)来访问数据库:

I'm using the following repository(single instance of it is used by all action methods of the controller) to access the DB:

public class ProductRepository : IProductRepository
    {
        private readonly EFDbContext _context = new EFDbContext();

        #region Implementation of IProductRepository       

       ....

        public void SaveProduct(Product product)
         {           
            if (product.ProductId == 0)
            {
                _context.Products.Add(product);
            }
            else
            {
                _context.Entry(product).State = EntityState.Modified;

            }

            _context.SaveChanges();
        }

....
}

这个仓库进行更新,因为它是在我使用的例子所示。

类别:

public class Product
    {       
        public int ProductId { get; set; }       
        public string Name { get; set; }      
        public string Description { get; set; }     
        public decimal Price { get; set; }
        public string Category { get; set; }
}

在更新产品的情况下,我得到异常具有相同的键在ObjectStateManager已经存在的对象。该ObjectStateManager不能用相同的密钥跟踪多个对象

In case of updating the product, I'm getting the exception "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key"

我知道,类似的问题已经被这里讨论,但我的问题是有点不同:

I know that the similar questions have been already discussed here but my question is a bit different:

为什么这个code 这是从实例采取不工作(虽然显得pretty简单和直接的)?自己做错了什么可能我做了或错过了什么。

Why this code which was taken from examples is not working (though it looks pretty simple and straightforward)? What wrong might I have done or missed something.

推荐答案

搜索小时的解决方案后,我发现一个似乎适合做的不够读后。

After searching for hours for a solution, I have found one that seems suitable after doing enough reading.

该修补程序是在这里:

<一个href=\"http://stackoverflow.com/questions/5672255/an-object-with-the-same-key-already-exists-in-the-objectstatemanager-the-objects\">An使用相同的密钥对象已经存在于ObjectStateManager。该ObjectStateManager不能使用相同的密钥跟踪多个对象

基本上,从上下文获取该记录,并呼吁:

Basically, fetch the record from the Context and call:

var currentProduct = _context.Products.Find(product.ProductId);    
_context.Entry(currentProduct).CurrentValues.SetValues(product);

这似乎是一个坏主意,这是我一直在我的previous运作痛恨EF,但cccording到拉吉斯拉夫Mrnka(谁apparnently回答#2每EF相关的问题),在这篇文章:

This seems like a bad idea and something I've always hated about EF in my previous workings, but cccording to Ladislav Mrnka (who apparnently answers every EF related question on Stackoverflow) in this post:

<一个href=\"http://stackoverflow.com/questions/3653009/entity-framework-and-connection-pooling/3653392#3653392\">Entity框架和连接池

EF将存储的实体的请求在内部,因此理想情况下,它已经在那里,它不会作出额外呼叫回数据库。

EF will store a request for an entity internally, so ideally, it will already be there and it won't be making an additional call back to the database.

问题的根本原因似乎是,一旦一个产品是从上下文获取,上下文是保持它的轨道,而这正是导致所有的麻烦。因此,合并更改回是唯一的办法。

The root cause of the problem seems to be that once a product is fetched from the Context, the context is keeping track of it and that's what is causing all the trouble. So merging your changes back in is the only way.

希望有所帮助。

这篇关于&QUOT;具有相同键的对象已经存在于ObjectStateManager ...&QUOT;设置实体状态时,修改会抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆