Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)?

like image 223
Kryszal Avatar asked Apr 17 '15 19:04

Kryszal


1 Answers

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.

like image 65
ion173 Avatar answered Sep 22 '22 04:09

ion173