title: Update entity in Entity Framework that return from cache
body: <p>I have two repositories (<code>Cache</code>, <code>EntityFramework</code>) for data processing. I want to get entity from <code>cache</code> and update in <code>EntityFramework</code>, for example error code:</p>
  
  <pre><code>User user = _cacheRepo.Get&lt;User&gt;(2);
  user.Name = "Qolzam";
  _entityFrameworkRpo.Update&lt;User&gt;(user);
  </code></pre>
  
  <p>ERORR</p>
  
  <blockquote>
    <p>Can't attach entity to update</p>
  </blockquote>
  
  <p>but on other hand this code works:</p>
  
  <pre><code>User user = _entityFrameworkRpo.Get&lt;User&gt;(2);
  user.Name = "Qolzam";
  _entityFrameworkRpo.Update&lt;User&gt;(user);
  </code></pre>
  
  <p>Succes: attach entity to update is ok</p>
  
  <p>[Update]</p>
  
  <pre><code>public T Update&lt;T&gt;(T item, bool commit = true) where T : class , IBaseEntity
    {
      var entry = this.Entry(item);
      if (entry != null)
      {
         entry.State = EntityState.Modified;
         entry.CurrentValues.SetValues(item);
      }
      else
      {
         this.Attach(item);
      }
      this.SaveChanges();
  
      return item;
    }
  </code></pre>
  
  <blockquote>
    <p>[Exception]</p>
  </blockquote>
  
  <p>System.InvalidOperationException was caught
    _HResult=-2146233079
    _message=Attaching an entity of type 'Domain.User' failed because another entity of the same type      already has the same primary key value. This can happen when using the 'Attach' method or setting the  state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.
    HResult=-2146233079
    IsTransient=false
    Message=Attaching an entity of type 'Domain.User' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.
    Source=EntityFramework
    StackTrace:
         at System.Data.Entity.Core.Objects.ObjectContext.VerifyRootForAdd(Boolean doAttach, String entitySetName, IEntityWrapper wrappedEntity, EntityEntry existingEntry, EntitySet&amp; entitySet, Boolean&amp; isNoOperation)
         at System.Data.Entity.Core.Objects.ObjectContext.AttachTo(String entitySetName, Object entity)
         at System.Data.Entity.Internal.Linq.InternalSet<code>1.&lt;&gt;c__DisplayClassa.&lt;Attach&gt;b__9()
         at System.Data.Entity.Internal.Linq.InternalSet</code>1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
         at System.Data.Entity.Internal.Linq.InternalSet<code>1.Attach(Object entity)
         at System.Data.Entity.Internal.InternalEntityEntry.set_State(EntityState value)
         at System.Data.Entity.Infrastructure.DbEntityEntry</code>1.set_State(EntityState value)</p>