实体对象不能由IEntityChangeTracker的多个实例被引用 [英] An entity object cannot be referenced by multiple instances of IEntityChangeTracker
问题描述
我用EF作为ORM。
我处置的ObjectContext
在每次请求。
我保存实体缓存层,因为我的服务获取大量的流量。
I save the entities in a cache layer, as my service gets lots of traffic.
我有时会得到错误的ObjectContext已经部署
对于一些实体,我从缓存中得到。
I got sometimes get the error objectContext already disposed
for some entities that I got from the cache.
我加入这个code从缓存中retrived的元素
I have added this code to elements that were retrived from the cache
if (maMDBEntities.Entry(group).State == EntityState.Detached)
{
maMDBEntities.Groups.Attach(group);
}
但现在我有时会收到此错误:
but now I sometimes get this error:
这是实体对象不能由IEntityChangeTracker的多个实例被引用。
An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
使用附加()
从首位不正确的解决方案?
Was using Attach()
an incorrect solution from the first place?
推荐答案
当我们告诉你在你的其他问题(<一个href=\"http://stackoverflow.com/questions/14724397/will-efattachentity-will-solve-objectcontext-is-already-desposed\">will EF ::附加(实体)将解决的ObjectContext已经desposed?),你必须将其连接到另一个上下文之前分离的实体!
As we told you in your other question (will EF::attach(entity) will solve objectContext is already desposed?), you have to detach the Entities before attaching it to another Context!
如果 maMDBEntities
从这个语境点视图一个新的上下文(不是之一,它装载的数据),该EntityState没有附加。所以你的检查是不够的。
If maMDBEntities
is a new Context (not the one, which loaded the data), the EntityState is not "attached" from this Contexts point-of-view. So your check is not sufficient.
本 maMDBEntities.Entry(集团).STATE == EntityState.Detached
将总是是一个方面,它没有加载正确实体。
This maMDBEntities.Entry(group).State == EntityState.Detached
will always be true for an Context, which did not load the Entity.
这篇关于实体对象不能由IEntityChangeTracker的多个实例被引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!