自我跟踪实体集并比较变化
我正在将 .net 应用程序作为我大学的一个项目,但我在自我跟踪实体集方面遇到了问题。我正在选择一条记录,将其通过 wcf 服务传递,进行一些更改,然后再次将其传递到服务器。我想比较更改的内容,因此我再次从上下文中选择相同的记录(这是具有旧值的记录)并进行比较。然后,在比较之后,我想在上下文中调用 ApplyChanges 与通过服务传递的实体,但我收到错误:
AcceptChanges 无法继续,因为该对象的键值与 ObjectStateManager 中的另一个对象冲突。在调用 AcceptChanges 之前确保键值是唯一的。
我想这是因为加载相同的实体两次(更改之前和更改之后进行比较)。有没有更好的方法来比较更改(我需要在另一个数据库表中创建更改历史记录)?如果两个客户端获取相同的记录,并且第一个客户端更改它并想要存储到数据库中,是否也会出现此错误?我使用.net 4.0。
Im making .net application as a project on my Univeristy and I have a problem with Self-Tracking Entity Sets. I'm selecting an record, passing it through a wcf service, make some changes and pass it to the server again. There I want to compare what was changed, so Im selecting the same record from context once a again (this is the record with old values) and comparing. Then, after comparing I want to call ApplyChanges on context with entity passed via the service, and Im getting an error:
AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.
I suppose it's because loading the same entity twice (before changes and after changes to compare). Is there any better way to compare changes(I need to make change history in another db table)? And wouldnt this error also apear if two clients gets the same record, and the first one changes it and wants to store into db? Im using .net 4.0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不应该是使用 STE 的方式。 STE 封装了很多逻辑,但它们无法处理 附加到上下文的重复键。
所以我可以想象两种解决方案:
SaveChanges
)以从ObjectStateManager
获取应用的更改。This is not supposed way to use STEs. STEs wrap a lot of logic but they are not able to handle duplicate keys attached to context.
So I can imagine two solutions:
SaveChanges
on context) to get applied changes fromObjectStateManager
.