如何强制触发 OnModelCreating 每个初始化的 DataContext

How to force fire OnModelCreating every DataContext initialized up(如何强制触发 OnModelCreating 每个初始化的 DataContext)

本文介绍了如何强制触发 OnModelCreating 每个初始化的 DataContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想触发 OnModelCreating 每个 new DataContext(new Entity()) ...但是当我创建一个表的连接时,它可以工作.当为另一个表创建连接时,OnModelCreating 不再工作,所以因为我得到了错误,

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,

实体类型<tableName>不是当前上下文模型的一部分.

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
    }
}

推荐答案

ta.speot.is 是正确的,OnModelCreating 只触发一次,因为 modelBuilder 被缓存了.有几种情况需要再次执行 OnModelCreating.例如,当跨会话实现多租户时,可能需要再次触发 OnModelCreating.

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.

首次创建模型构建器时,EF 会对其进行缓存以提高性能.它使用 IDbModelCacheKeyProvider.CacheKey 进行缓存.OnModelCreating 在找不到与 CacheKey 关联的 Cache 时触发.因此,要再次触发 OnModelCreating,必须更改 IDbModelCacheKeyProvider.CacheKey.

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.

要更改缓存键,DbContext 类必须实现 IDbModelCacheKeyProvider.当在 IDbModelCacheKeyProvider 的 CacheKey 属性中返回新的缓存键时,OnModelCreating 事件再次触发.

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.

例如看下面的代码块:

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

当tenantID 更改时,它会强制执行OnModelCreating,因为缓存可能无法用于新的tenantID.

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

注意:只有在非常紧急的情况下才应该更改 CacheKey.频繁更改 CacheKey 会降低性能.

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

这篇关于如何强制触发 OnModelCreating 每个初始化的 DataContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何强制触发 OnModelCreating 每个初始化的 DataContext

基础教程推荐