实体框架 6. 禁用模型缓存 [英] Entity Framework 6. Disable ModelCaching

查看:19
本文介绍了实体框架 6. 禁用模型缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,谷歌,我无法在谷歌上搜索它.文档

OK, Google, I am not able to googling it. Documentation says

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

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

所以确认它.但我找不到办法.我必须禁用缓存,因为我想通过一个模型从多个具有相同结构的日志表中获取数据,所以我的代码看起来像

and SO confirms it. But i can't find way to do it. I have to disable caching because i want to get data from several log table with same structure via just one model, so my code looks like

logTableNames.ForEach(n =>
{
    using (var context = new LogContext(n))
    {
        Console.WriteLine($"Project: {n} -- {context.Logs.Count()} rows.
");
    }
});

并配置

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Log>().ToTable(_tableName);
}

但OnModelCreating"方法只调用了一次,我无法重新配置表映射.那么也许还有另一种真正的方式来完成我的任务?

but 'OnModelCreating' method called just once and i can not reconfigure table mapping. So maybe there is another true-way to do my task?

推荐答案

这里是实体框架团队项目经理的回答.

Here is the answer from the Program Manager of the Entity Framework team.

罗文米勒 (MSFT)

我们在 CTP5 中删除了 CacheForContextType,我们原本打算将其用于当人们想在相同的环境中使用相同的上下文时使用AppDomain 具有不同的模型.问题是它会创建在每次初始化时建模,并且不允许任何方式缓存系列模型并选择在每个模型中使用哪一个初始化.模型创建很昂贵,所以我们想推广一个更好的模式.

We removed CacheForContextType in CTP5, we originally intended it to be used when folks wanted to use the same context in the same AppDomain with different models. The issue is that it would create the model on every initialization and didn't allow any way to cache a series of models and choose which one to use during each initialization. Model creation is expensive so we wanted to promote a better pattern.

我们推荐的模式是外部创建模型构建器 ->DbDatabaseMapping -> 您要使用的每个模型的 DbModel.这DbModel 应该被缓存并用于创建上下文实例.这ModelBuilder -> DbModel 工作流程有点乱,类名不是很好,他们会为 RTM 整理.

The pattern we recommend is to externally create a ModelBuilder -> DbDatabaseMapping -> DbModel for each model you want to use. The DbModel should be cached and used to create context instances. The ModelBuilder -> DbModel workflow is a little messy and the class names aren't great, they will be tidied up for RTM.

这篇关于实体框架 6. 禁用模型缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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