如何强制触发 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Can I initialize a BCL immutable collection using braces?(我可以使用大括号初始化BCL不可变集合吗?)
The type initializer for #39;System.Data.Entity.Internal.AppConfig#39; threw an exception on a Sub Website(“System.Data.Entity.Internal.AppConfig的类型初始化程序在子网站上引发异常)
Possible to initialize multiple variables from a tuple?(可以从一个元组初始化多个变量吗?)
error loading database initializer with EF6(使用 EF6 加载数据库初始化程序时出错)
The type appear in two structurally incompatible initializations within a single LINQ to Entities query(该类型出现在单个 LINQ to Entities 查询中的两个结构不兼容的初始化中)
Does Stacklt;gt; constructor reverse the stack when being initialized from other one?(是否堆栈lt;gt;构造函数在从另一个初始化时反转堆栈?)