I am using SimpletInjector
to inject the entity framework
context in controller using following code:
private static void Initializer(Container container)
{
container.RegisterSingle<IDbContext, SpotterContext>();
container.RegisterSingle<IUnitOfWork, UnitOfWork>();
}
I am using the injected context to execute stored procedure which will have only UPDATE
or DELETE
statements using the following code:
context.Database.ExecuteSqlCommand(<spname>, <params array>);
The problem I am facing is that the changes made by the stored procedure in database are not getting reflected in the context and when accessing the updated data using the context, I am getting the old data and not the updated data.
How to update the context
after executing a stored procedure to get latest data from database?
Had to solve exactly the same problem recently. As it appears, Reload()
method of context' Entry
member works just fine.
Sorry, I don't know C#, so can't post a code sample on that language. In VB.NET it looks like this:
Dim CurClient As Client = Ctx.Clients.Where(Function(cl) cl.Id = ClientId).First()
Ctx.Entry(Of Client)(CurClient).Reload()
Here, Ctx is an EF context.
The only thing I haven't tested yet is whether this method also reloads records from child tables, if they exist.