OnModelCreating only called once even for new contexts

c# entity-framework entity-framework-6


I have many identical yet uniquely different SQL server tables. I'm attempting to reuse the same database context for each application I'm developing in code first EF6 and supply the table name to the context function Object() { [native code] }.

Although the database context is constructed from fresh each time, the OnModelCreating procedure is only called once while the function Object() { [native code] } is called each time. How can this be reset?

I tried utilising AsNoTracking after reading that ModelCaching should be disabled, but I couldn't figure out how to accomplish that or if it was the best course of action. In fact, MSDN states that "this caching may be stopped by setting the ModelCaching property on the supplied ModelBuidler[sic]," but such property is not present.

My DB context is as follows:

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)

To utilise it, use my code:

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();

The right TABLEA, TABLEB, and TABLEC are always present in the running table, which is supplied to the function Object() { [native code] } of the database context; however, the OnModelCreating method is only called once, for the first loop item, ensuring that the SQL produced by the query object always selects from TABLEA:

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

*Please excuse any incorrect code; I had to rename certain variables since they were confidential company information.

3/9/2018 3:26:06 PM

Popular Answer

OnModelCreating will only be called once; this is the default.

OnModelCreating documentation states.

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.
3/9/2018 2:38:17 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow