How to force fire OnModelCreating every DataContext initialized up

好久不见. 提交于 2020-01-11 07:01:08

问题


I want to fire OnModelCreating every new DataContext(new Entity()) ... But when i create a connection of a table , it works. When create a connection for another table, OnModelCreating doesnt work again, so because i got error,

the entity type <tableName> is not part of the model for the current context.

public class DataContext : DbContext
{
    private BaseEntity _entity;

    public DataContext(BaseEntity entity)
    {
        Database.Connection.ConnectionString = Parameters.ConnectionString;

        _entity = entity;
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        _entity.Map(modelBuilder); // this is dynamic fluent api here
    }
}

回答1:


ta.speot.is is right that OnModelCreating fires only once because modelBuilder is cached. There are several cases when OnModelCreating is required to execute again. For example when multi-tenancy is implemented across sessions then one may require to fire OnModelCreating again.

When modelBuilder is created first time, then EF caches it to increase performance. it is cached using IDbModelCacheKeyProvider.CacheKey. OnModelCreating fires when it does not find Cache associated with CacheKey. So to get OnModelCreating fired again IDbModelCacheKeyProvider.CacheKey must be changed.

To change cache key, DbContext class must implement IDbModelCacheKeyProvider. When new cache key is returned In CacheKey property of IDbModelCacheKeyProvider then OnModelCreating event fires again.

For example see following code block:

public class TenantContext : DbContext, IDbModelCacheKeyProvider
{
    string IDbModelCacheKeyProvider.CacheKey {
        get { return tenentID.ToString(); }
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

When tenantID is changed, it forces OnModelCreating to execute because cache might not be available for new tenantID.

Note: CacheKey should be changed only if it is very urgent. Frequent change in CacheKey will decrease performance.




回答2:


The manual has got you covered!

DbContext.OnModelCreating Method (DbModelBuilder)

...

Remarks

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 [sic], but note that this can seriously degrade performance. More control over caching is provided through use of the DbModelBuilder and DbContextFactory classes directly.

Although if it's possible you might want to explore a generic DbContext like public class DataContext<TBaseEntity> : DbContext. This will lead you to an architecture where you have one DataContext type per TBaseEntity type.



来源:https://stackoverflow.com/questions/46035249/how-to-force-fire-onmodelcreating-every-datacontext-initialized-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!