Skip to main content
removeu 19 caracteres do conteúdo
Fonte Link
Jéf Bueno
  • 69,8mil
  • 22
  • 134
  • 244

Olá, nãoNão estou conseguindo atualizar meu objeto. Criei o método da seguinte forma:

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.<Attach>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

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

Obrigado!

Olá, não estou conseguindo atualizar meu objeto. Criei o método da seguinte forma:

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.<Attach>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

Obrigado!

Não estou conseguindo atualizar meu objeto. Criei o método da seguinte forma:


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
Fonte Link
Raphael
  • 489
  • 1
  • 5
  • 20

Entity Framework: Erro ao Atualizar Objeto

Olá, não estou conseguindo atualizar meu objeto. Criei o método da seguinte forma:

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

A seguinte exceção foi lançada:

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.<Attach>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

Está falando que está dando conflito de chave, não entendi, pois já que se trata de update e não insert.

No contexto já adicionei o objeto cliente, do qual quero atualizar:

public DbSet<Cliente> Clientes { get; set; }

Obrigado!