自跟踪实体 - AcceptChanges 无法继续,因为对象的键值与 ObjectStateManager 中的另一个对象冲突
我已经被这个问题困扰了一个多星期了。希望有人能指出我正确的方向。
我首先简要描述我的架构。
资产 1--->1 地址 *-->1 区域 *-->1 区域 *-->1 国家/地区
包 1-->*
使用自我跟踪实体 (STE) + WCF 的资产。
步骤:
- 调用数据存储以获取资产列表。
- 调用数据存储以获取包列表。
- 用户选择一个包并为其分配一些资产。
- 保存包。
在步骤 2 中,调用使用地址的预先加载。
from p in context.Assets.Include("Address.Area.Region.Country")
这是尝试调用时的错误
context.Packages.ApplyChanges(package)
AcceptChanges 无法继续,因为 对象的键值冲突 中的另一个对象 对象状态管理器。确保 调用前键值是唯一的 接受更改。
编辑
经过一番探听,我发现这是一个 STE 问题。问题是您无法保留包含同一实体的多个实例的图表,如所述 此处。这是我的问题。
如何将实体添加到我的实体中 收藏。新实体可能有 包含相同内容的相关实体 键作为集合中已有的键。 即添加一个可能包含的新资产 相同的地址、地区、地区或 国家实体。
以下是我的限制:
- 我必须使用导航集合,因为它会影响 UI。
- 我无法预取将涉及的所有实体,因为数据集太大。
- 我必须能够随时拍摄实体的快照,以便保留历史记录并使用它来“撤消”任何更改。
我知道 Diego B Vega 建议的可能解决方案,但这些不是我可以用于我的解决方案的选项。还有其他想法吗?
I've been stuck with this problem for over a week now. Hopefully some one can point me in the right direction.
I start with a brief description of my schema.
Asset 1--->1 Address *-->1 Area *-->1 Region *-->1 Country
Package 1-->* Asset
Using Self Tracking Entity (STE) + WCF.
Steps:
- Call data store for a list of assets.
- Call data store for a list of packages.
- User selects a package and assign some assets to it.
- Save package.
In step 2, the call uses eager loading of Addresses.
from p in context.Assets.Include("Address.Area.Region.Country")
This is the error when attempting to call
context.Packages.ApplyChanges(package)
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.
EDIT
After snooping around, i found that this is a STE issue. The problem being you cannot persist a graph that contains multiple instances of the same entity as outlined here. Here is my question.
How can I add an entity to my entity
collection. The new entity may have
related entities that contain the same
key as one already in the collection.
I.e. Add a new asset that may contain
the same address, area, region or
country entity.
Here are my constrains:
- I must use the navigational collection because it affect the UI.
- I cannot pre-fetch all entities that will be involved because the data set is simply too large.
- I must be able to take snap-shots of the entity at anytime in order to keep a history and use it to "undo" any changes.
I am aware of the possible solutions suggested by Diego B Vega, but those are not options i can use for my solution. Has anyone any other ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
仅供参考,我写了一篇博文,其中对我在 EF 论坛中已经回复的内容提出了一些额外的建议。 此处 是博客文章。
FYI, I wrote a blog post with some additional suggestions to what I had already responded in the EF Forums. Here is the blog post.
你是否考虑过放弃 ORM 并回到正常访问,如果你明白我的意思:-)
不是开玩笑 - 当你努力解决像这样的一个问题时(这比任何事情都更像 ORM bug)否则)您可以推出自己的 5-10 个函数来执行正常的存储过程调用和更简单的数据类型转换,然后您将回到完全控制状态,并且不会被库所困扰,这些库将需要另外 5 年的时间才能稳定下来。
特别是因为您似乎有非常干净的架构 - 这意味着非常简单的查询和直接的更新。
Have you considered just giving up on ORM-s and going back to normal access, if you know what I mean :-)
Not kidding - by the time you wrestle with one single issue like that one (which smells like ORM bug more than anything else) you could have rolled out your own 5-10 functions to do normal sproc calls and easier data type conversion and then you are back to being in full control and and not stuck by libraries which are going to take another like 5yr to stabilize.
Especially since you seem to have very clean schema - meaning quite simple queries and straight forward updates.
我遇到了同样的问题,最后找到了解决方案。基本思想是防止某些导航类类型附加到 ObjectContext。
这是我所做的:
I encountered the same issue and finally came up with a solution. The basic idea is to prevent certain navigational class type from attaching to the ObjectContext.
Here is what I did:
我通过设置重置外键 ID 解决了这个问题,这需要在保存之前将导航值设置为 null。
...像这样:
I solved this by Setting the resetting the Foreign Key Ids, which required setting the Navigation value to null, before saving.
...something like this:
我收到此错误是因为我正在删除实体的记录,重新播种,然后用新数据重新填充该实体。
由于该实体启用了自我跟踪,因此它不允许添加具有相同密钥的记录,即使具有该密钥的记录之前已被删除。
由于在这种情况下我不需要自我跟踪,因此我将其禁用为:
I was getting this error because I was deleting the records of an entity, reseeding it and then refilling the entity with new data.
As self tracking was enabled on this entity, it did not allow adding the record with the same key, even though a record with that key had been deleted earlier.
Since I did not need self tracking in this situation, I disabled it as: