可能重复:
我在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;
}
}有人能帮忙解决这个问题吗?
发布于 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
https://stackoverflow.com/questions/6534849
复制相似问题