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

发布于 2024-11-17 21:56:16 字数 1529 浏览 8 评论 0原文

可能的重复:
。 ObjectStateManager 中已存在具有相同键的对象ObjectStateManager 无法跟踪具有相同键的多个对象。

我已将实体框架与 GridView 的 ObjectDataSource 一起使用。当我尝试使用 updatemethod 时,我收到了运行时错误消息

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

这是我的 aspx 文件代码:

    <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 层代码文件

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

有谁可以帮助解决这个问题吗?

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

I have used Entity Framework with ObjectDataSource for GridView. while i have tried with updatemethod i got the run time error message

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

Here is my aspx file code:

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

Here is my Code File of DA Layer

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

Is there anyone who can help to sort out this issue ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

农村范ル 2024-11-24 21:56: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

How are you managing the context (ObjectContext) - does BusinessEntityBase.Entities is shared across multiple requests? This can be an issue - because object retrieved from one request can conflict when you are trying to update the object from other request (and so attach will fail). See this link for possible solution: http://dotnetslackers.com/articles/ado_net/Managing-Entity-Framework-ObjectContext-lifespan-and-scope-in-n-layered-ASP-NET-applications.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文