首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ObjectStateManager中已经存在具有相同键的对象。ObjectStateManager无法用相同的键跟踪多个对象。

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

Stack Overflow用户
提问于 2011-06-30 12:37:37
回答 1查看 2.4K关注 0票数 0

可能重复:

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

我在ObjectDataSource中为GridView使用了实体框架。当我尝试使用updatemethod时,我得到了运行时错误消息。

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

以下是我的aspx文件代码:

代码语言:javascript
复制
    <asp:ObjectDataSource ID="odsCustomerList" runat="server" DataObjectTypeName="EF.POCO.Customer"
                TypeName="EF.BusinessLayer.CustomerMaster" SelectMethod="ReadAllCustomer" SortParameterName="sortExpression"
                ConflictDetection="CompareAllValues" OldValuesParameterFormatString="orig{0}"  
                UpdateMethod="UpdateCustomer" DeleteMethod="DeleteCustomer">
    </asp:ObjectDataSource>

这是DA层的代码文件

代码语言:javascript
复制
     public void UpdateCustomer(Customer customer, Customer origCustomer)
    {
        try
        {

            BusinessEntityBase.Entities.Customers.MergeOption = System.Data.Objects.MergeOption.NoTracking;
            BusinessEntityBase.Entities.Customers.Attach(origCustomer);
            BusinessEntityBase.Entities.ApplyCurrentValues("Customer", customer);
            BusinessEntityBase.Entities.SaveChanges();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

有人能帮忙解决这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-30 13:05:16

如何管理上下文(ObjectContext) -在多个请求之间共享BusinessEntityBase.Entities吗?这可能是一个问题--因为从一个请求中检索到的对象在尝试从其他请求更新对象时可能会发生冲突(因此attach将失败)。有关可能的解决方案,请参阅此链接:http://dotnetslackers.com/articles/ado_net/Managing-Entity-Framework-ObjectContext-lifespan-and-scope-in-n-layered-ASP-NET-applications.aspx

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6534849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档