title: An entity object cannot be referenced by multiple instances of IEntityChangeTracker even though I detached it
body: <p>I'm using EF. I save entities into cache.</p>
  
  <p>I detach each entity before putting into cache, </p>
  
  <p>and attaching it to a new objectContext after getting from cache.</p>
  
  <p>I'm using <code>using</code> statement at a high point (in my BL class, upon every request)</p>
  
  <p>Yet I get this error:</p>
  
  <pre><code>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></pre>
  
  <p>Here is my detach + attach code:
  The <code>using</code> inject the objectContext to here:</p>
  
  <pre><code>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;
      }
  </code></pre>
  
  <p>The first time (getting fresh entity from DB into cache): </p>
  
  <p>the entity is detached and attached to the same contextObjec.</p>
  
  <p>The second time (getting the entity from the cache), </p>
  
  <p>it's only attached to a new contextObject.</p>
  
  <p>Do I have to do any additional step? Is my detach missing anything?</p>