6
var contactado = ContactadoRepositorio.ObterListaContactadoPorCodigo(procedimentoVM.CodContactadoPessoa); //obtenho o contactado pelo id obtido;
foreach (var c in contactado)
{
var contactadoPessoa = new ContactadoPessoa()
{
Codigo = c.Codigo,
CodigoPessoa = c.CodigoPessoa,
Nome = c.Nome,
CodigoProcedimento = procedimento.Codigo
};
ContactadoRepositorio.Atualizar(contactadoPessoa);
}
//Com o objeto obtido eu atualizo as propriedades que eu quero salvar no BD
public void Atualizar(ContactadoPessoa contactadoPessoa)
{
this.Context.ContactadoPessoas.Attach(contactadoPessoa);
this.Context.Entry(contactadoPessoa).State = EntityState.Modified;
this.Context.SaveChanges();
}
//Metodo para Atualizar e salvar no BD
When running the code I get the following error:
Attaching an Entity of type 'Forte.Rastreador.Model.Contactadopessoa' 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
What is going on? could improve your question a little? Some error?
– PauloHDSousa
which error is returned?
– WeezHard
Attaching an Entity of type 'Forte.Rastreador.Model.Contactadopessoa' 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.
– Hans Miller
just that mistake!
– Hans Miller
I edited your question to include the bug. Edit your question and include the method code
ObterListaContactadoPorCodigo
and the classContactadoPessoa
. What you want to update isCodigoProcedimento
?– ramaral