自我跟踪实体集并比较变化

发布于 2024-10-12 15:25:03 字数 448 浏览 12 评论 0原文

我正在将 .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 技术交流群。

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

发布评论

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

评论(1

执笏见 2024-10-19 15:25:03

这不应该是使用 STE 的方式。 STE 封装了很多逻辑,但它们无法处理 附加到上下文的重复键

所以我可以想象两种解决方案:

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:

  • Do not load entity for comparing changes. Instead apply changes from your STE and handle SavingChanges event (or override SaveChanges on context) to get applied changes from ObjectStateManager.
  • Try to detach loaded entity before you apply changes from STE.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文