Entity Framework: Object Update Error

Asked

Viewed 1,186 times

0

I am unable to update my object. I created the method as follows:

public void Atualizar(T obj)
{
    banco.Entry(obj).State = EntityState.Modified;
    banco.SaveChanges();
}

The following exception was made:

Attaching an entity of type 'ProjetoTeste.Domain.Entities.Clientes.Cliente'
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.

em System.Data.Entity.Core.Objects.ObjectContext.VerifyRootForAdd(Boolean doAttach, String entitySetName, IEntityWrapper wrappedEntity, EntityEntry existingEntry, EntitySet& entitySet, Boolean& isNoOperation)
   em System.Data.Entity.Core.Objects.ObjectContext.AttachTo(String entitySetName, Object entity)
   em System.Data.Entity.Internal.Linq.InternalSet`1.c__DisplayClassa.b__9()
   em System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
   em System.Data.Entity.Internal.Linq.InternalSet`1.Attach(Object entity)
   em System.Data.Entity.Internal.InternalEntityEntry.set_State(EntityState value)
   em System.Data.Entity.Infrastructure.DbEntityEntry`1.set_State(EntityState value)
   em ProjetoTeste.Infra.Data.Repositories.RepositoryBase`1.Atualizar(T obj) na D:\Informática\Raphael\Projetos VS2015\ProjetoTeste\Codigo\ProjetoTeste\ProjetoTeste.Data\Repositories\RepositoryBase.cs:linha 18

You are talking about key conflict, I do not understand, since it is about update and not Insert.

In context I have already added the client object, which I want to update:

public DbSet<Cliente> Clientes { get; set; }
  • The error is not in this code snippet, probably in the "Update" method. Dbcontext is "watching" the entity that returned, it seems that you are adding another entity with the same id.

  • Thank you Fernando! I saw that the object is out of context because I was trying to take the object of the parameter and pass to the update method. So I did a trick like this: I recovered the object I want to update with Getbyid() and passed the values of the parameter object to the object that was returned by Getbyid() so that EF does not lose the reference. Is there any method to add this object to the context?

  • I prefer to always search for the entity from the database, and in the controller (in the post) I use a Viewmodel.The method to add in context is this (Entitystate.Modified, or Attach) and should have worked if the entity came from the Binder model. What may also be is that you are using the same context in all requests (is the context static?) which is bad practice too.

  • Ah ta, got it. Thanks Fernando! But the context is not static. There is no way to mark your answer here as solved?

  • by the comments no. As you did not put the other part of the code (that instance the context, and the entity) I did not want to create a response. If you put the code, I can add an answer to help other people.

2 answers

1

0

You need to add the obj in the DbSet<T>

Example:

public void AtualizarCliente(Cliente obj)
{
      banco.Cliente.Add(obj);//Você adiciona o obj modificado no cliente
      Atualizar(obj);//Agora você pode mandar salvar.
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.