Name editing is confirmed in the database but does not occur in the list

3

I am editing a database column and so far everything is fine, but when I load the table that contains this column using Include, it simply does not update the " Driver ", ID in the relationship but in the listing continues the name of the old registered (actually the one that was registered never changes).

Follow the query:

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:

Here'showthedriverisnotactuallybeingloadedcorrectly:

Follow the code of references 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);
    
asked by anonymous 10.06.2015 / 22:07

1 answer

2

By default, queries are cached for each instance of context.

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

There are some ways to get the data up to date.

1- Finish the context:

What I use most is to give my context in my context right after using it. I usually place my context object 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 Refresh of the context object to update it with data coming from the database.

context.Refresh(RefreshMode.StoreWins, seuObjetoEntity);
    
11.06.2015 / 15:42