C# “一个实体对象不能被 IEntityChangeTracker 的多个实例引用。”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16934376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"An entity object cannot be referenced by multiple instances of IEntityChangeTracker."
提问by Hina
I am using MYSql server as a database behind my windows form application. There are two schemas in my db that I have to throw entries into. I have created two context objects one for each schema. When I use contextA which is on schema1 all the entries are done perfectly but when I use contextB I get this exception. Does it have something to do with MySql Driver.
我使用 MYSql 服务器作为 Windows 窗体应用程序背后的数据库。我的数据库中有两个模式,我必须将条目放入其中。我为每个模式创建了两个上下文对象。当我使用 schema1 上的 contextA 时,所有条目都完成了,但是当我使用 contextB 时,我得到了这个异常。和MySql Driver有关系吗?
回答by qujck
It is almost certainly caused by proxies and change tracking. Disable these features in both constructors and see if it resolves your issue.
这几乎可以肯定是由代理和更改跟踪引起的。在两个构造函数中禁用这些功能,看看它是否能解决您的问题。
public class contextA : DbContext
{
public contextA()
{
Configuration.ProxyCreationEnabled = false;
}
}
回答by Not loved
This error says you are trying to attach an entity to your context but its already attached to another one.
此错误表示您正在尝试将一个实体附加到您的上下文,但它已附加到另一个实体。
My suspicion is that this is probably not a direct reference but perhaps one of the navigation properties on your context contains an entity which is attached to the other context. In my opinion (from what you have described) seperate contexts should only really be used if they are disconnected object structures, eg they have no FKs between the contexts.
我怀疑这可能不是直接引用,但您的上下文中的导航属性之一可能包含一个附加到另一个上下文的实体。在我看来(根据您所描述的),只有当它们是断开连接的对象结构时才应该真正使用单独的上下文,例如它们在上下文之间没有 FK。
The other thing to avoid is to ensure that for each unit of work you only use one instance of each context. If you try and use an entity from another context instance this error will also occur.
另一件要避免的事情是确保对于每个工作单元,您只使用每个上下文的一个实例。如果您尝试使用来自另一个上下文实例的实体,也会发生此错误。
EDIT:
编辑:
IDs are generally a better idea to use if you want to maintain scope outside of the current context. You can reattach entities to EF so you can add them in the way you are describing but you have to make sure the original context is disposed or the entity is detached and then manually attach it to the new context with something like the following:
如果您想维护当前上下文之外的范围,通常使用 ID 是一个更好的主意。您可以将实体重新附加到 EF,以便您可以按照您描述的方式添加它们,但您必须确保原始上下文已释放或实体已分离,然后手动将其附加到新上下文,如下所示:
public DbEntityEntry<T> EnsureAttachedEF(T entity)
{
var e = m_Context.Entry(entity);
if (e.State == EntityState.Detached)
{
m_Context.Set<T>().Attach(entity);
e = m_Context.Entry(entity);
}
return e;
}
This however is quite a bit of work so using IDs is generally a better idea.
然而,这是相当多的工作,因此使用 ID 通常是一个更好的主意。
回答by Justin
Moving comment to anwser:
将评论移至 anwser:
It seems you maybe working with the entity outside of the EF context. This would cause the changes to the entity to not be tracked. At the same time, EF would cache this entity and if you try to attach the entity to the context, it will see it already exists and will throw an error.
看来您可能在 EF 上下文之外使用实体。这将导致无法跟踪对实体的更改。同时,EF 会缓存这个实体,如果你尝试将实体附加到上下文,它会看到它已经存在并抛出错误。
You could use the NoTrackingoption in EF to stop EF from caching the entity if you are working with it outside of the context.
NoTracking如果您在上下文之外使用实体,则可以使用EF 中的选项来阻止 EF 缓存实体。

