5

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

Vinblad
  • 676
  • 1
  • 7
  • 18

4 Answers4

2

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.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
2

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

Nikos Tsokos
  • 3,226
  • 2
  • 34
  • 41
  • This is actually a good answer. Just add .AsNoTracking() as part of your query and entity framework will not rely on in-memory instances of the objects. – Nikola Schou Sep 21 '16 at 22:34
1

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:

Refresh ObjectContext or recreate it to reflect changes made to the database?

Community
  • 1
  • 1
Yakimych
  • 17,612
  • 7
  • 52
  • 69
  • 6
    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 Oct 06 '10 at 20:27
  • 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 Oct 07 '10 at 05:45
  • any plans to fix this answer ? – phil soady Apr 27 '13 at 12:17
0

FWIW, creating a new (anonymous) object in the query also forces a round trip to the database:

' queries from memory    
context.Entities.FirstOrDefault()

' queries from db
context.Entities.Select(Function(x) New With {p.ID, p.Name}).FirstOrDefault()

Please forgive the VB, it's my native language :)

Elmer
  • 135
  • 1
  • 7