1

I have two repositories (Cache, EntityFramework) for data processing. I want to get entity from cache and update in EntityFramework, for example error code:

User user = _cacheRepo.Get<User>(2);
user.Name = "Qolzam";
_entityFrameworkRpo.Update<User>(user);

ERORR

Can't attach entity to update

but on other hand this code works:

User user = _entityFrameworkRpo.Get<User>(2);
user.Name = "Qolzam";
_entityFrameworkRpo.Update<User>(user);

Succes: attach entity to update is ok

[Update]

public T Update<T>(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;
  }

[Exception]

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& entitySet, Boolean& isNoOperation) at System.Data.Entity.Core.Objects.ObjectContext.AttachTo(String entitySetName, Object entity) at System.Data.Entity.Internal.Linq.InternalSet1.<>c__DisplayClassa.<Attach>b__9() at System.Data.Entity.Internal.Linq.InternalSet1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) at System.Data.Entity.Internal.Linq.InternalSet1.Attach(Object entity) at System.Data.Entity.Internal.InternalEntityEntry.set_State(EntityState value) at System.Data.Entity.Infrastructure.DbEntityEntry1.set_State(EntityState value)

Amir Movahedi
  • 1,802
  • 3
  • 29
  • 52
  • How your `_cacheRepo.Get` and `_entityFrameworkRpo.Update` look like? – Sergey Berezovskiy Sep 12 '14 at 06:43
  • @SergeyBerezovskiy _cacheRepo.Get() is simple get object by id from RuntimeCache`if not exist id get from entityframework and insert to cache and EntityFramework update is simple update use generic repository http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application – Amir Movahedi Sep 12 '14 at 06:53
  • IIRC, you cannot share an entity between repositories. Try to detach 'user' from 'cacheRepo' before updating it in 'efRepo' – RX_DID_RX Sep 12 '14 at 12:27
  • You might have a look at my answer on [ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value](http://stackoverflow.com/questions/23201907/asp-net-mvc-attaching-an-entity-of-type-modelname-failed-because-another-ent/39557606#39557606). – Murat Yıldız Sep 18 '16 at 12:36

1 Answers1

0

I share my update method in repository.I think you can use this method.

protected override void PersistUpdateInternal(IStoreObject storeObject)
{
    if (storeObject == null)
        throw new NullReferenceException("storeObject");

    var original = GetOriginalEntity(storeObject);
    if (original != null)
    {
        var entry = _context.Entry(original);
        entry.CurrentValues.SetValues(storeObject);
    }
    else
    {
        GetDbSet().Attach((T)storeObject);
        _context.Entry(storeObject).State = EntityState.Modified;
    }

}
protected T GetOriginalEntity(IStoreObject storeObject)
{
    var objectContext = ((IObjectContextAdapter)_context).ObjectContext;
    var objectSet = objectContext.CreateObjectSet<T>();

    var entitySet = objectSet.EntitySet;
    var keyMembers = entitySet.ElementType.KeyMembers;

    Expression<Func<T, bool>> predicate = i => true;
    foreach (var keyMember in keyMembers)
    {
        var propertyInfo = typeof(T).GetProperty(keyMember.Name);

        var pe = Expression.Parameter(typeof(T), "i");
        var left = Expression.PropertyOrField(pe, keyMember.Name);
        var right = Expression.Constant(propertyInfo.GetValue(storeObject), propertyInfo.PropertyType);
        var body = Expression.Equal(left, right);
        var whereExpression = Expression.Lambda<Func<T, bool>>(body, pe);
        predicate = predicate.And(whereExpression);
    }

    var original = GetDbSet().Local.FirstOrDefault(predicate.Compile());

    return original;
}
Osman
  • 16
  • 1