How to make compiled cached queries outlive the context?
-
20-12-2019 - |
Question
Since Entity Framework 5 queries are compiled and cached by default. This boosts performance for repeated queries.
However context is designed to be a shortlived object and is disposed after processing the request.
Therefore we have many repeated queries running under different contexts. We can precompile queries manually.
May be EF caches compiled queries in static instances or is there any way to make queries cache outlive the context?
Solution
EF cached queries stay cached independent of the context. Disposing context won't dispose the query cache.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow