Name editing is confirmed in the database but does not happen in the listing

Asked

Viewed 49 times

3

I am editing a column of the database and so far everything goes very well, but when I load the table that contains this column using Include, simply does not update the "Chauffeur", exchange the ID in the relationship but in the listing continues the name of the old registered (actually what was registered never exchange).

Follow the consultation:

return View(context.Veiculos.Include(v => v.Motorista).Where(v => v.EmpresaID == userId && !v.Excluido).OrderByDescending(c => c.Ano).ToList());

In the database the two columns of the listing are with the same driver, follow the print that proves:

banco de dados

Here shows how the driver is not actually being loaded properly:

listagem

Follow the reference code between the two Models:

User Class:

[Display(Name = "Vehicles", ResourceType = typeof(Resources.Language))]
public virtual ICollection<Veiculo> VeiculosEmpresa { get; set; }

Vehicle Class:

[Required]
public Guid MotoristaID { get; set; }

[Display(Name = "Driver", ResourceType = typeof(Resources.Language))]
public virtual ApplicationUser Motorista { get; set; }

Modelbuilder:

modelBuilder.Entity<Veiculo>().HasRequired(x => x.Motorista).WithMany(x => x.VeiculosMotorista);
modelBuilder.Entity<Veiculo>().HasRequired(x => x.Empresa).WithMany(x => x.VeiculosEmpresa);

1 answer

2


By default, queries are cached for each context instance.

Even if you save data in context with Savechanges the given cache is maintained for that context instance.

There are some ways to have the data updated.

1- Finalise the context:

What I use most is giving Dysplasia in my context right after using it. I usually put my object context in a block using:

using (MeuContextoEntities context = new MeuContextoEntities())
{
    //minhas operações com o contexto.
}

2 - If you are using a global context object (which I do not recommend), you can use the method Refresh context object to update it with data coming from the database.

context.Refresh(RefreshMode.StoreWins, seuObjetoEntity);

Browser other questions tagged

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