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

后端 未结 4 462
猫巷女王i
猫巷女王i 2020-12-31 04:36

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.

4条回答
  •  没有蜡笔的小新
    2020-12-31 04:52

    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.

提交回复
热议问题