Data update issues with Entity Framework with relational data

Asked

Viewed 27 times

0

I have a Query table that has two foreign keys (Client and Procedure).

to add a new query I can normally, but when editing it does not change my Client or Procedure the other fields modifies normally.

public static void EditarConsulta(Consulta entrada)
    {
        try
        {
            using (ConsultorioContext ctx = new ConsultorioContext())
            {
                ctx.Entry(entrada).State = EntityState.Modified;
                ctx.SaveChanges();
            }
        }
        catch (Exception)
        {

        }
    }

other ways I’ve tried:

ctx.ChangeTracker.Entries<Consulta>().First(x => x.Entity == entrada).State = EntityState.Modified;

ctx.Entry<Consulta>(entrada).State = System.Data.Entity.EntityState.Modified;

1 answer

1


Lucas, you are starting the context within the method, in which case the Entity does not recognize the entity original.

Try to find the original entity and set the values on top of it using the SetValue. Something like:

var entidadeExistente = ctx.Consulta.Find(entidadeAlterada.Id);
ctb.Entry(entidadeExistente).CurrentValues.SetValues(entidadeAlterada);

Hugs

Browser other questions tagged

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