4

Is there a way in Entity Framework 4.0 to bypass the object cache for a single LINQ query?

I want to be able to execute a query and know that I'm getting the absolute latest from the database even if that object has been retrieved earlier in the request and has been stored in cache.

1 Answer 1

3

You must configure your query or object set to force materialization of result set instead of using already materialized entities from identity map.

context.YourObjectSet.MergeOption = MergeOption.OverwriteChanges;
// now execute the query as many times as you want

or

var query = ...;
((ObjectQuery<YourEntity>)query).MergeOption = MergeOption.OverwriteChanges;
Sign up to request clarification or add additional context in comments.

3 Comments

+1 Good to know that. MergeOption is a somewhat unexpected name to enforce cache overrides, but ah well, it works. I was experimenting with ObjectContext.Refresh with RefreshMode.StoreWins and was baffled to see that that did not work as I expected.
It did NOT work for me (EF6.0). I could not find MergeOption property. I guess there were some breaking changes in EF. This answer was useful: stackoverflow.com/a/4911591/1131855
@MaximEliseev: MergeOption is a feature of original ObjectContext API. You are using newer API.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.