0

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

我已将实体框架与 ObjectDataSource 一起用于 GridView。当我尝试使用 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;
        }
    }

有没有人可以帮助解决这个问题?

4

1 回答 1

1

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

于 2011-06-30T13:05:16.117 回答