23

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.

6
  • I'm afraid, you cannot do this. And, maybe this is good, because in other case on this global context refresh whole your database will be selected to your application. Sep 16, 2013 at 13:40
  • I'm wondering because DBContext take always data from Database directly also if you have cashed. You have to refesh your data and not the dataContext I mean reload the data from DB? Sep 16, 2013 at 13:42
  • @BassamAlugili, I've clarified the problem, please take a look at the edit. Sep 16, 2013 at 13:47
  • 1
    With SignalR this is definitly possible. Check out this blog post from Brij: techbrij.com/…
    – Marco
    Sep 16, 2013 at 13:51
  • @Serv, That looks a good tool but I don't have problem with being notified about changes. I can't force EF to load updated data from database. It won't update properties of entities that have changed. Sep 16, 2013 at 13:56

2 Answers 2

17

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.

10
  • 1
    I am using the same context for repositories and the context is registered by IoC. Sep 16, 2013 at 14:39
  • 1
    @Mert With an IoC library you can still have a short-lived context. For example, using Autofac and Web API you can do builder.Register(c => context).As<IIntranetContext>().InstancePerApiRequest();
    – user247702
    Sep 16, 2013 at 14:41
  • The problem is that I would need to reinitialize all the repo's because they depend on the context. Service layer interacts with repo's not context. Sep 16, 2013 at 14:43
  • 1
    @IvanFerrerVilla Sorry for not responding earlier, I must have missed the notification. Usually you'll map the entities immediately after retrieving them, to business objects or view models (or something else). To update, you either retrieve the entity again and modify the properties (easier), or you manually attach an entity with just the Primary Key field(s) set, fill in the properties and mark them as having changed (no select query executed). I haven't actively used EF for a few years now, so I'm not up to speed with regards to today's best practices.
    – user247702
    Oct 7, 2016 at 16:09
  • 1
    @IvanFerrerVilla See stackoverflow.com/a/17442858/247702 for an example of the manual attach method.
    – user247702
    Oct 7, 2016 at 16:11
3
db = new DbContext())
var context= ((Infrastructure.IObjectContextAdapter)db).ObjectContext;
context.Refresh(Core.Objects.RefreshMode.StoreWins, context.ObjectStateManager.GetObjectStateEntries(EntityState.Unchanged | EntityState.Modified))
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.