views:

50

answers:

2

Hi!

I have this applikation that is actually two applications, a webapplication and a console application. The console application is used as a scheduled task on the windows machine and is executed 3 times a day to to some recurring work. Both application uses the same Model and repository that is placed in a seperate projekt (class library). The problem is that if the console application need to make som changes to the database it updates the model entity and save the changes to database but when this happens the context in the webbapplication is unaware of this and therefore the object context is not refreshed with the new/updated data and the user of the application can not see the changes.

My question is: Is there a way to tell the objectcontext to always load data from the database, either on the hole objectcontext or for a specific query?

/Regards Vinblad

+2  A: 

Whenever you run something like

context.Entities.FirstOrDefault()

or whatever query against the context, the data is actually fetched from the database, so you shouldn't be having a problem.

What is your ObjectContext lifetime in the webapp? The ObjectContext is a UnitOfWork, so it should be only created to fetch/write/update data and disposed quickly afterwards. You can find a similar question here:

http://stackoverflow.com/questions/3298057/refresh-objectcontext-or-recreate-it-to-reflect-changes-made-to-the-database/3298841

Yakimych
That is not true. If you run your query, modify your data in DB and run the query again on the same context you will not receive new data. ObjectContext implements several patterns - this one is called Identity Map which means that entity of the same key is loaded only once.
Ladislav Mrnka
Thanks Yakimych for the info, the link you provided me with got me in the right direction, it turned out that I needed support for PerWebRequest in Unity (i used this implementatino that seems to work: http://weblogs.asp.net/rashid/archive/2009/02/15/asp-net-mvc-unity-and-common-service-locator.aspx)
Vinblad
@Ladislav - thanks for the correction! @Vinblad - glad I could help ;)
Yakimych
+1  A: 

I don't think you should have this problem in web application. ObjectContext in web application should be created per request so only requests processing during update should be affected.

Anyway there are few methods wich can force ObjectContext to reload data. Queries and load functions allow passing MergeOption which should be able to overwrite current data. But the most interesting should be Refresh method especially with this application.

Ladislav Mrnka