Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

I write a software ( huge software about 200 form which works on a network ), every form have Many queries.

now I need to clear EF6 cache. I need to load data from database, not context cache.


what is the easy and time-saving way to clear this cache? there is any method to apply it to all over EF one time without repeating it in all forms?

What I have tried:

i searched and found these methods but all of them Not suitable for applied once and difficult to carry out many times.

Disable Tracking using AsNoTracking()
Throw away the DbContext and create a new one
Use an ObjectQuery instead of a DBQuery and set MergeOptions
Refresh the Entities
Detach the Entities
Call GetDatabaseValues to get the updated values for a single Entity
Posted
Updated 30-Apr-18 18:49pm

This question makes me wonder how you're creating a DbContext instance and using it. To "clear the cache" is to just Dispose the context instance and create another one.

You most certainly should NOT create a DbContext instance and just hold onto it for the lifetime of your application.
 
Share this answer
 
While "Dispose and create new" is generally the best advice, there are edge cases. For example, if a method is going to be doing *alot* of work, but that method is getting a context passed to it, and is incapable of creating a new context (it does not have sufficient information). BEing able to "flush" specific DbSets (and handle side effects) would be a powerful feature...
 
Share this answer
 
Comments
Richard Deeming 3-Oct-22 11:43am    
That may be the case; but your comment is not a solution to this already-solved question, and is definitely not a reason to resurrect a thread from four years ago!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900