Error updating

1

I am working on a windows forms app, Entity Framework, I have an error when doing the Update.

public void Update(T entity)
    {
        //Context.Set<T>().Attach(entity);
        Context.Entry(entity).State = EntityState.Modified;
        TrySaveChanges();
    }
  

Error:   Placement of an entity of type 'POS.Domain.Tallas' failed because another entity of the same type already has the same primary key value. This can occur when the 'Attach' method is used or the state of an entity is set to "No change" or "change" if any entity in the chart has conflicting fundamental values. This may be because some entities are new and have not yet received the generated database key values. In this case, use the 'Add' method or 'added' entity state to trace the chart below, set the state of non-new entities to "Unchanged" or "modified", as appropriate.

You have to see how the Context is configured

public BaseRepository(PosContext context,
        bool autoDetectChangesEnabled = false,
        bool proxyCreationEnabled = false)
    {
        this.Context = context;
        this.Context.Configuration.AutoDetectChangesEnabled = autoDetectChangesEnabled;
        this.Context.Configuration.ProxyCreationEnabled = proxyCreationEnabled;
    }

I check if there is a record in the db

public class TallaRepository : BaseRepository<Talla>, ITalla<Talla>
{
    public TallaRepository(
        bool autodetectChangesEnabled = false,
        bool proxyCreationEnabled = false) : base(
            new PosContext(),
            autodetectChangesEnabled, proxyCreationEnabled)
    { }

    public bool Existe(int codigo)
    {
        int result = Context.Tallas.Where(x => x.TallaId == codigo).Count();
        if (result == 0)
            return false;
        else
            return true;
    }
}

According to that result I do the Update or Create

public void Create(Talla entity)
    {
        if (_tallaRepository.Existe(entity.TallaId))
            _tallaRepository.Update(entity);
        else
            _tallaRepository.Create(entity);
    }

I have the error in the Update that I show you lines above

    
asked by Pedro Ávila 06.06.2016 в 17:18
source

2 answers

2

I managed to solve it, first I had to create an Exist method that returns the entity (if it exists, I returned the entity and I update it and if it is null Create).

public bool Exist(Expression<Func<Talla, bool>> predicate)
    {
        return Context.Tallas.Any(predicate);
    }

I apply according to the result of Exist

public void Create(Talla entity)
    {
        if(_tallaRepository.Exist(x=>x.TallaId == entity.TallaId))
            _tallaRepository.UpdateTalla(entity);
        else
            _tallaRepository.Create(entity);
    }

UpdateTalla

public void UpdateTalla(Talla entity)
    {
        var local = Context.Set<Talla>()
            .Local
            .FirstOrDefault(x => x.TallaId == entity.TallaId);
        if (local != null)
            Context.Entry(local).State = EntityState.Detached;

        Context.Entry(entity).State = EntityState.Modified;
        TrySaveChanges();
    }

In UpdateTalla, if I'm not mistaken I remove it from the context to be able to update. What is the reason for this?

    
answered by 07.06.2016 / 21:05
source
0

Taking the following model as an example

public class Person
{
    public int Id { get; set; } // primary key
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

verify having the primary key defined so you can perform:

Context.Entry(entity).State = EntityState.Modified;
Context.SaveChanges();

This way you will be correctly pointing the record you want to modify.

    
answered by 06.06.2016 в 18:47