如何在 Entity Framework 6 中禁用模型缓存(代码优先方法) [英] How to disable model caching in Entity Framework 6 (Code First approach)

查看:48
本文介绍了如何在 Entity Framework 6 中禁用模型缓存(代码优先方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下 MSDN 文档我们可以阅读:

<块引用>

然后缓存该上下文的模型,并用于应用程序域中上下文的所有进一步实例.可以通过在给定的 ModelBuidler 上设置 ModelCaching 属性来禁用此缓存,但请注意,这会严重降低性能.

问题是模型构建器不包含任何名为 ModelCaching 的属性.

如何禁用模型缓存(例如,在运行时更改模型配置)?

解决方案

我有同样的问题:一个数据库上下文,2 个或更多不同的数据库模型(仅表名不同)

我的 EF6 解决方案:仍然可以使用 db 模型的内部实体框架缓存,但通过在派生的 DbContext 上实现 IDbModelCacheKeyProvider 接口来区分同一 DbContext 上的 DbModel..p>

MSDN 文档在这里:https://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.idbmodelcachekeyprovider(v=vs.113).aspx它说:

<块引用>

在您的上下文中实现此接口以使用自定义逻辑来计算用于在缓存中查找已创建模型的键.此接口允许您拥有可与同一 AppDomain 中的不同模型一起使用的单个上下文类型,或使用相同模型的多个上下文类型.

希望对某人有所帮助.

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

解决方案

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.

这篇关于如何在 Entity Framework 6 中禁用模型缓存(代码优先方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆