How do I update my data in the table using the Entityframework?

Asked

Viewed 4,566 times

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?

  • which error is returned?

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

  • just that mistake!

  • I edited your question to include the bug. Edit your question and include the method code ObterListaContactadoPorCodigo and the class ContactadoPessoa. What you want to update is CodigoProcedimento?

4 answers

3

You don’t have to Attach for this case. Modify to:

public void Atualizar(ContactadoPessoa contactadoPessoa)
{
    // this.Context.ContactadoPessoas.Attach(contactadoPessoa);
    this.Context.Entry(contactadoPessoa).State = EntityState.Modified;
    this.Context.SaveChanges();
}

2

The simplest thing is to do it this way:

db is the instantiated context so: nomeContext db = new nomeContext();

Then do this:

var registro = db.NomeDaTabela.FirstOrDefault(c => c.chavePrimaria);

registro.nome_do_campo1 = 'novo conteudo1';
registro.nome_do_campo2 = 'novo conteudo2';
.
.
.

db.Entry(registro).State = EntityState.Modified;
db.SaveChanges();

The most common error is that we want to create an object (record) like this from scratch without using the object that was located with Firstordefault(). If we assign the id and everything else in this record without it being located, the object we create is flawed and gives way.

  • I forgot to say that without locating the correct record that we want to update, with Firstordefault(), we will not be able to update it using only its ID contained in the Table.

1

You use attach only to attach an entity to the context that is not yet being monitored by it.

Since your entity is already in context and you are modifying its properties, you can save the changes in context directly

this.Context.Entry(contactadoPessoa).State = EntityState.Modified;
this.Context.SaveChanges();

Or simply use Entitystate implicitly:

this.Context.SaveChanges();

-1

Assuming the CODIGO column is the primary key of the Contactadopessoas table.Try to do so

var contactado = ContactadoRepositorio.ObterListaContactadoPorCodigo(procedimentoVM.CodContactadoPessoa); //obtenho o contactado pelo id obtido;

 foreach (var c in contactado)
            {
                var contactadoPessoa = this.Context.ContactadoPessoas.FirstOrDefault(cp => cp.Codigo == c.Codigo);

                    contactadoPessoa.CodigoPessoa = c.CodigoPessoa,
                    contactadoPessoa.Nome = c.Nome,
                    contactadoPessoa.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();

    }

Browser other questions tagged

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