How to disable model caching in Entity Framework 6 (Code First approach)

c# caching ef-code-first ef-model-builder entity-framework-6

Question

Following MSDN documentation we can read:

The model for that context is then cached and is for all further instances of the context in the app domain. This caching can be disabled by setting the ModelCaching property on the given ModelBuidler, but note that this can seriously degrade performance.

The problem is the model builder does not contain any property named ModelCaching.

How it is possible to disable the model caching (e.g. for changing model configuration in a run-time)?

1
15
4/18/2015 11:10:23 AM

Popular Answer

I have the same kind of issue: one db context, 2 or more different db models (different by table names, only)

My solution for EF6: One can still use the internal Entity Framework caching of db model but make a differentiation between DbModel(s) on the same DbContext by implementing IDbModelCacheKeyProvider Interface on derived DbContext.

MSDN doc is here: https://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.idbmodelcachekeyprovider(v=vs.113).aspx And it says:

Implement this interface on your context to use custom logic to calculate the key used to lookup an already created model in the cache. This interface allows you to have a single context type that can be used with different models in the same AppDomain, or multiple context types that use the same model.

Hope it helps someone.

6
11/1/2017 7:39:12 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow