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

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

问题描述

我遵循了一些例子(包括Pro ASP.NET MVC 3和Professional ASP.NET MVC 3等),使用EF 4.1创建简单的ASP.NET MVC 3应用程序(由于我是新的技术)。



我正在使用以下存储库(它的单一实例由控制器的所有操作方法使用)访问DB:

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

#region实现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;组; }
public string Name {get;组; }
public string描述{get;组; }
public decimal价格{get;组; }
public string Category {get;组; }
}

如果更新产品,我得到例外An ObjectStateManager中已经存在具有相同密钥的对象,ObjectStateManager无法跟踪具有相同密钥的多个对象



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



为什么这个代码从示例中获取不起作用(尽管它看起来很简单直观)?可能我做错了什么或错过了什么。

解决方案

在寻找解决方案的几个小时后,我找到了一个似乎合适的做足够的阅读后。



修复程序在这里:



ObjectStateManager中已经存在具有相同密钥的对象。 ObjectStateManager无法跟踪具有相同键的多个对象



基本上,从上下文中获取记录,并调用:

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

这似乎是一个坏主意,我以前在工作中总是讨厌EF,根据Ladislav Mrnka(他在Stackoverflow中适用于每个EF相关问题):



实体框架和连接池



EF将内部存储对实体的请求,所以理想情况下,它已经在那里,它不会再额外调用回数据库。



问题的根本原因似乎是,一旦从上下文中获取产品,上下文就会跟踪它,这是导致所有麻烦的原因。所以合并您的更改是唯一的方法。



希望有所帮助。


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();
        }

....
}

This repository performs updating as it was shown in the examples I used.

Product class:

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; }
}

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:

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.

The fix is here:

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key

Basically, fetch the record from the Context and call:

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

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:

Entity Framework and Connection Pooling

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.

Hope that helps.

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

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