Entity Framework: How to detect external changes to database

c# database entity-framework stored-procedures

Question

I have a stored procedure that changes lots of data in the database. This stored procedure is called from the application that at the same time uses EF for data operations.

So I click a button, stored procedure is run at the database, data is changed and EF shows old data to the user.

Is there a way to force the DbContext or ObjectContext to refresh data from database? ObjectContext.Refresh() may be the solution but I do not want to call this method for every single table that may be changed. I want all the tables to be refreshed in one move.

I am using Entity Framework 5, targeting .NET 4.0

EDIT: Added data is available but modification on existing data is not reflected by EF. I see the newly added records but I cannot see the changes I made to existing records.

1
20
9/16/2013 2:29:02 PM

Accepted Answer

Your DbContext should be short-lived. Create it, run your query, and dispose it.

using (var context = new MyProject.DbContext())
{
    // run your query here
}

Don't keep your context around. That way you won't have any issues with old data.

16
9/16/2013 2:33:50 PM

Popular Answer

db = new DbContext())
var context= ((Infrastructure.IObjectContextAdapter)db).ObjectContext;
context.Refresh(Core.Objects.RefreshMode.StoreWins, context.ObjectStateManager.GetObjectStateEntries(EntityState.Unchanged | EntityState.Modified))


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow