实体对象不能由即使我脱离它IEntityChangeTracker的多个实例被引用 [英] An entity object cannot be referenced by multiple instances of IEntityChangeTracker even though I detached it

查看:1963
本文介绍了实体对象不能由即使我脱离它IEntityChangeTracker的多个实例被引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用EF。我保存实体到缓存。

I'm using EF. I save entities into cache.

我取下每个实体投入缓存之前,

I detach each entity before putting into cache,

和它从高速缓存获得后附加到新的ObjectContext

and attaching it to a new objectContext after getting from cache.

我用使用语句处于高点(在我的BL类,在每一个请求)

I'm using using statement at a high point (in my BL class, upon every request)

不过,我得到这个错误:

Yet I get this error:

System.InvalidOperationException: An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
at System.Data.Objects.ObjectContext.VerifyContextForAddOrAttach(IEntityWrapper wrappedEntity)
at System.Data.Objects.ObjectContext.AttachSingleObject(IEntityWrapper wrappedEntity, EntitySet entitySet, String argumentName)
at System.Data.Objects.ObjectContext.AttachTo(String entitySetName, Object entity)
at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
at System.Data.Entity.Internal.Linq.InternalSet`1.Attach(Object entity)
at System.Data.Entity.DbSet`1.Attach(TEntity entity)
at Conduit.Mam.ClientService.DAL.MamData.MamDataRepository.GetMamData(String stamp, MaMDBEntities maMDBEntities) in d:\Builds\TFS\9\221\Sources\Server\ClientServices\Dev\ClientService 1.3\ServiceDAL\MamData\MamDataRepository.cs:line 56
at Conduit.Mam.ClientService.BLL.MamDataManager.GetMamData() in d:\Builds\TFS\9\221\Sources\Server\ClientServices\Dev\ClientService 1.3\Conduit.Mam.ClientService.BLL\MamDataManager.cs:line 57
at Conduit.Mam.ClientService.BLL.AppsDataManager.GetClientRelevantsApps(List`1 appsData) in d:\Builds\TFS\9\221\Sources\Server\ClientServices\Dev\ClientService 1.3\Conduit.Mam.ClientService.BLL\AppsDataManager.cs:line 107
at Conduit.Mam.ClientService.Service.Controllers.ClientDataController.GetAppsData() in d:\Builds\TFS\9\221\Sources\Server\ClientServices\Dev\ClientService 1.3\Conduit.Mam.ClientService.Service\Controllers\ClientDataController.cs:line 58   

下面是我的分离+附加code:
使用注入的ObjectContext到这里:

Here is my detach + attach code: The using inject the objectContext to here:

public static Group GetMamData(string stamp, MaMDBEntities maMDBEntities)
    {
        Group group = MamDataCacheManager.GetMamData(stamp);

        if (group == null)
        {
            //was not found in the cache
            //check for aveilable test with status 'start' - 1
            group = GetGroupsFromDb(stamp, maMDBEntities);

            if (group != null)
            {
                maMDBEntities.Entry(group).State = EntityState.Detached;

                MamDataCacheManager.InsertMamData(stamp, group);

            }
        }

        //option B: attache a new context
        if (maMDBEntities.Entry(group).State == EntityState.Detached)
        {
            maMDBEntities.Groups.Attach(group);
        }

        return group;
    }

第一次(获得新鲜的实体从数据库到缓存):

The first time (getting fresh entity from DB into cache):

实体被分离并连接到同一contextObjec

the entity is detached and attached to the same contextObjec.

第二次(正从高速缓存的实体),

The second time (getting the entity from the cache),

它只是连接到一个新的contextObject。

it's only attached to a new contextObject.

我必须做任何额外的步骤?是我的分离缺少什么?

Do I have to do any additional step? Is my detach missing anything?

推荐答案

分离只影响传递给方法的特定对象。如果拆离的对象有对象上下文中的对象,这些对象不沾边。

Detach only affects the specific object passed to the method. If the object being detached has related objects in the object context, those objects are not detached.

这篇关于实体对象不能由即使我脱离它IEntityChangeTracker的多个实例被引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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