gpt4 book ai didi

c# - Entity Framework 6. 禁用模型缓存

转载 作者:行者123 更新时间:2023-11-30 23:21:51 27 4
gpt4 key购买 nike

好的,谷歌,我无法用谷歌搜索它。 Documentation

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

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

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

并配置

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

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

最佳答案

下面是 Entity Framework 团队项目经理的回答。

罗文·米勒 (MSFT)

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.

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.

关于c# - Entity Framework 6. 禁用模型缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39093542/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com