ObjectStateManager 中已存在具有相同键的对象。 ObjectStateManager 无法跟踪具有相同键的多个对象
可能的重复:
。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您如何管理上下文 (ObjectContext) - BusinessEntityBase.Entities 是否在多个请求之间共享?这可能是一个问题 - 因为当您尝试从另一个请求更新对象时,从一个请求检索的对象可能会发生冲突(因此
attach
将失败)。请参阅此链接以获取可能的解决方案:http://dotnetslackers.com/articles/ado_net/Managing-Entity-Framework-ObjectContext-lifespan-and-scope-in-n-layered-ASP-NET-applications.aspxHow 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 soattach
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