Rob
Rob

Reputation: 11798

How to avoid data caching when querying the database?

I'm having a strange problem with ASP.NET MVC4 and Entity Framework 5: The web application I'm building retrieves data from a database and sends it as Json into a viewmodel on the page, from which it then gets presented in a table on the page. The data represents the state of some datapoints that change every now and then.

Now I observed the following behaviour:

I can't breakpoint the controller method that retrieves the data, as it only occurs on the production server, but when I look into the Json data I can see that it actually is old data. So it seems like EF is caching the retrieved data. This is a serious problem as we use this web application for industrial monitoring purposes and therefore need to rely on up-to-date data.

Has anyone encountered the same issue? Any help on this is greatly appreciated!

Upvotes: 0

Views: 104

Answers (1)

Damon
Damon

Reputation: 3010

I don't know entity framework that well but I think this has something to do with change tracking. I'd try disabling it to force EF to re-query the DB, I think (and others please can correct me if I'm wrong) but unless SaveChanges has been called on an ObjectContext if you re-query the same data the database won't be queried again.

I've used MergeOption = MergeOption.NoTracking (on the ObjectSet) to turn it off in my project.

Upvotes: 1

Related Questions