如何在objectstatemanager中删除重复的实体 [英] How to remove duplicate entities in the objectstatemanager

查看:39
本文介绍了如何在objectstatemanager中删除重复的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调用 AcceptChanges 时,出现以下错误:

AcceptChanges 无法继续,因为对象的键值与 ObjectStateManager 中的另一个对象冲突.在调用 AcceptChanges 之前,请确保键值是唯一的.

现在我已经在网上搜索了很多,但是没有找到我想要的答案.

我知道他们是有意设计的,因此您应该避免使用ID代替实体对象来添加已经存在的实体,但是我处于无法做到的境地.

我有一个导入和导出,这是通用的,我可以给任何实体对象,它将使它成为xml格式,反之,我可以给任何xml并获得正确的实体.我无法使用序列化程序,因为我必须以自定义格式制作xml.

问题出在当我有更深层的嵌套实体可以在之前添加到主实体中时,我无法当场检查它,因此它们被添加了.导致同一实体可能多次添加的问题.

当我走得更远并且希望应用更改时,就会发生错误.

现在,考虑到防止多次添加同一实体"问题,我别无选择,添加后必须处理.

如何从 ObjectStateManager 中删除重复的实体,以便我可以调用应用更改?

解决方案

如何从ObjectStateManager中删除重复的实体,所以我可以打电话申请更改吗?

只能通过搜索 ObjectStateManager 并解决重复项来手动进行.这就是为什么在将实体添加到 ObjectContext ObjectStateManager.ObjectStateManagerChanged 事件处理程序中之前应解决此问题的原因.

无论如何,您问的是错误的问题.即使您从 ObjectStateManager 删除重复的实体也无法解决问题.删除实体只会破坏您的对象图.您还需要修复对已删除实体的所有引用,并将它们指向剩余的单个实体实例.

此问题通常由序列化/反序列化直接处理,如果您自己编写,也应该这样做.

When calling AcceptChanges I get the following 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.

Now I've searched the web for quite a bit, but didn't find the answer I was looking for.

I know they made it by design so that you should prevent from adding an entity that already exists by using the ID instead of an entity object but I'm in a situation where i simply cannot do that.

I have an import and an export, which is generic, I can give any entity object and it will make it an xml format, and in reverse I can give any xml and get the right entity. I couldn't use a serializer because I had to make the xml in a custom format.

The problem lies when I have deeper nested entities that could have been added to the main entity before already , I cannot check it on the spot so they get added. causing the problem that the same entity might get added multiple times.

And when I'm further ahead and I wish to apply changes, the error happens.

Now consider that I don't have a choice in the matter of preventing the "adding of the same entity multiple times"-problem and I have to deal with it after they have been added.

How can I delete duplicate entities from the ObjectStateManager, so that I can call apply changes?

解决方案

How can I delete duplicate entities from the ObjectStateManager, so that I can call apply changes?

Only manually by searching the ObjectStateManager and resolving duplicates. That is the reason why you should resolve that issue prior to adding entities to ObjectContext or in ObjectStateManager.ObjectStateManagerChanged event handler.

Anyway you are asking wrong question. Even if you delete duplicate entities from ObjectStateManager it will not solve the problem. Deleting entities will just break your object graphs. You also need to fix all references to deleted entities and point them to single remaining entity instance.

This issue is usually handled directly by serialization / deserialization and if you write it yourselves you should do it as well.

这篇关于如何在objectstatemanager中删除重复的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆