record does not update with EF

Asked

Viewed 30 times

0

I have a bit of a problem, on the screen, I change the value of the field, and when saved, it does not generate the error, and also does not save. The flow follows, as if there were no problem that object is like this

clientes.endereco

implemented the method in this way:

var db = contexto;
db.Entry(obj).State = EntityState.Modified;

in an earlier attempt, t.

var clienteAtualizar = (from c in db.Clientes.Include(c => c.Endereco)
                                             .Include(c => c.Endereco.Cidade)
                                             .Include(c => c.Endereco.Estado)
                       where c.ClienteId.Equals(obj.ClienteId)
                       select c).FirstOrDefault();

        clienteAtualizar.Celular = obj.Celular;
        clienteAtualizar.ClienteId = obj.ClienteId;
        clienteAtualizar.Cpf = obj.Cpf;
        clienteAtualizar.Email = obj.Email;
        clienteAtualizar.Endereco = obj.Endereco;
        clienteAtualizar.Endereco.EnderecoId = obj.Endereco.EnderecoId;
        clienteAtualizar.Endereco.CidadeId = obj.Endereco.CidadeId;
        clienteAtualizar.Endereco.Cidade = obj.Endereco.Cidade;
        clienteAtualizar.Endereco.Cidade.Estado = obj.Endereco.Cidade.Estado;
        clienteAtualizar.Endereco.Cidade.EstadoId = obj.Endereco.Cidade.EstadoId;
        clienteAtualizar.Endereco.EstadoId = obj.Endereco.EstadoId;
        clienteAtualizar.Endereco.Estado = obj.Endereco.Estado;
        clienteAtualizar.Nome = obj.Nome;
        clienteAtualizar.Rg = obj.Rg;
        clienteAtualizar.TelFixo = obj.TelFixo;

        var entry = db.Entry(clienteAtualizar);

        db.Entry(clienteAtualizar).State = EntityState.Modified;

but he duplicated the record in the son classes.

Does anyone have an idea how to fix this?

1 answer

0

Lacked a contexto.SaveChanges(); under the db.Entry(obj).State = EntityState.Modified;

  • missed or not, it is because I am using Unit of work. But even so, I did a test, calling the savechanges() in the class it does not save the address entity changes, only the client entity

Browser other questions tagged

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