Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make sure Entity framework always reads from database?

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

like image 502
Vinblad Avatar asked Oct 06 '10 10:10

Vinblad


2 Answers

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.

like image 73
Ladislav Mrnka Avatar answered Sep 29 '22 06:09

Ladislav Mrnka


By Using a DbSet you can you can also make use of the .AsNoTracking() method.

like image 27
Nikos Tsokos Avatar answered Sep 29 '22 06:09

Nikos Tsokos