OnModelCreating即使对于新上下文也仅调用一次 [英] OnModelCreating only called once even for new contexts

查看:379
本文介绍了OnModelCreating即使对于新上下文也仅调用一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个相同但内容不同的SQL Server表.在编写代码优先EF6程序时,我试图为每个数据库重用相同的数据库上下文,并将表名传递给上下文构造函数.

I have multiple SQL server tables that are the same, but differ in content. In writing a code first EF6 program I am trying to reuse the same db context for each and pass in the table name to the context constructor.

但是,每次都调用构造函数时,尽管每次都从new创建db上下文,但OnModelCreating方法仅被调用一次.我该如何重置?

However, while the constructor is being called every time, the OnModelCreating method is only being called once despite the db context being created from new every time. How do I reset this?

我已经尝试过使用AsNoTracking,并且阅读了禁用ModelCaching的内容,但是找不到如何执行此操作或这是否是最佳方法. MSDN甚至说可以通过在给定的ModelBuidler [sic]上设置ModelCaching属性来禁用此缓存,"但它不存在.

I have tried using AsNoTracking and I read along the lines of disabling ModelCaching but couldn't find out how to do this or whether this was the best approach. MSDN even says 'this caching can be disabled by setting the ModelCaching property on the given ModelBuidler[sic],' but it's not there.

这是我的数据库上下文:

This is my DB Context:

public partial class MissingContext : DbContext
{
    private string tableName = "";
    public MissingContext(string tableName) : base("name=MissingContext")
    {
        this.tableName = tableName;
    }

    public virtual DbSet<MissingData> MissingDataSet { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<MissingData>()
           .ToTable(this.tableName);
    }
}

这是我使用它的代码:

List<MissingData> missingData=null;
string[] inputTables="TABLEA;TABLEB;TABLEC".Split(';');
foreach (string table in inputTables)
{
    logger.Info($"Processing {table}");
    missingData = null;

    MissingContext db = new MissingContext(table);
    var query = from d in db.MissingDataSet.AsNoTracking()
                select d;
    missingData = query.ToList();
}

在运行中,表始终具有正确的TABLEA,TABLEB,TABLEC,并将其传递给db上下文构造函数,但是OnModelCreating仅在第一个循环项中调用一次,因此查询对象生成的SQL始终会选择来自TABLEA:

In running, table always has the correct TABLEA, TABLEB, TABLEC and it is passed in to the db context constructor, however the OnModelCreating is only called once for the very first loop item so the SQL generated by the query object always selects from TABLEA:

SELECT 
[Extent1].[id] AS [id], 
[Extent1].[OrganisationName] AS [OrganisationName] 
FROM [dbo].[**TABLEA**] AS [Extent1]

*抱歉,如果任何代码看起来有误,我会重命名一些变量,因为它们对业务敏感.

*apologies if any code looks wrong, I rename some variables as they are business sensitive.

推荐答案

OnModelCreating仅会被调用一次,这是默认行为.

OnModelCreating will be called only once that's default behaviour.

根据 通常,在创建派生上下文的第一个实例时,仅调用一次此方法.然后将缓存该上下文的模型,并将其用于应用程序域中该上下文的所有其他实例.可以通过在给定的ModelBuidler上设置ModelCaching属性来禁用此缓存,但是请注意,这会严重降低性能.直接使用DbModelBuilder和DbContextFactory类可以提供对缓存的更多控制.

Typically, this method is called only once when the first instance of a derived context is created. 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. More control over caching is provided through use of the DbModelBuilder and DbContextFactory classes directly.

这篇关于OnModelCreating即使对于新上下文也仅调用一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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