简体   繁体   English

ASP.NET Core OnModelCreating强制在每个请求中触发

[英]ASP.NET Core OnModelCreating force to fire in every request

ASP.NET Core OnModelCreating force to fire in every request ASP.NET Core OnModelCreating强制在每个请求中触发

I have ASP.NET Core (1.1.0) and SQL Server database. 我有ASP.NET Core(1.1.0)和SQL Server数据库。 In login page I have a form with a select box with name of companies. 在登录页面中,我有一个带有带有公司名称的选择框的表单。

For example: 例如:

  • Company 1 公司1
  • Company 2 公司2

If a user selects the the first "Company 1", I need to rename all the tables with a company prefix: 如果用户选择第一个“ Company 1”,则需要使用公司前缀重命名所有表:

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    foreach (var entity in builder.Model.GetEntityTypes())
    {
       entity.Relational().TableName = "Company 1" + entity.DisplayName();
    }
}

It's working perfectly fine, but the problems is when user selects the wrong company, or logs out of the system and logs back in with a different company, OnModelCreating never gets called again and I can't reconfigure the dbContext. 它工作正常,但是问题是当用户选择了错误的公司,或者注销系统并使用其他公司重新登录时,OnModelCreating再也不会被调用,而且我无法重新配置dbContext。

Is it possible to force OnModelCreating for every request? 是否可以为每个请求强制执行OnModelCreating?

Or change table name after dbContext initilization? 还是在dbContext初始化后更改表名?

Thanks! 谢谢!

It's wrong way. 这是错误的方式。 OnModelCreating runs when EF creates/describes model from your classes. 当EF从您的类创建/描述模型时,将运行OnModelCreating There are a lot of reflection and type manipulation here, including validating relations, comparing to real DB (and check if migrations are needed) and so on. 这里有很多反射和类型操作,包括验证关系,与真实数据库进行比较(并检查是否需要迁移)等等。 Do you really need to run all this stuff on every request??? 您是否真的需要在每个请求上都运行所有这些东西???

Do not recreate DbContext for every request. 不要为每个请求重新创建DbContext。 Create several DbContexts and choose one of them (created once!) at request start. 创建多个DbContext,并在请求开始时选择其中一个(创建一次!)。

For example: 例如:

Make your main DbContext abstract, and create as many CompanyXDbContext as needed. 使您的主要DbContext抽象,并根据需要创建尽可能多的CompanyXDbContext。 They all should inherit from base class, providing simple table prefix to it (in ctor). 它们都应继承自基类,并为基类提供简单的表前缀(在ctor中)。

Everywhere in your code, when you need DbContext, ask for abstract/base class from DI. 在代码中的任何地方,当您需要DbContext时,请向DI索取抽象/基类。

In Startup , register your abstract DbContext with factory method for choosing real one depending on current Request variables/params, or write custom middleware that creates correct instance based on current Request. Startup ,使用工厂方法注册抽象的DbContext,以根据当前的Request变量/参数选择真实的DbContext,或编写自定义中间件,该中间件根据当前Request创建正确的实例。


If you have a LOT of CompanyX, then you may need something other instead of many pre-coded CompanyXDbContext classes, something with runtime class creating or may be generics... But main idea is still unchanged - do not recreate model again and again on every request. 如果您有很多CompanyX,则可能需要其他东西,而不是许多预编码的CompanyXDbContext类,而某些东西已经创建了运行时类,或者可能是泛型的……但是主要思想仍然没有改变-不要一次又一次地重新创建模型每个请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何强制启动OnModelCreating每个初始化的DataContext - How to force fire OnModelCreating every DataContext initialized up 黑页或404 Asp.NET Core中的每个请求 - Black page or 404 every request in Asp.NET core EF Core 6.0 onModelCreating 在运行 ASP.NET Core Application 时运行(迁移之外) - EF Core 6.0 onModelCreating running when running ASP.NET Core Application (outside the Migration) 使用 Asp.Net Core 强制语言环境 - Force locale with Asp.Net Core 在 ASP.NET MVC Core 中强制注销 - Force logout in ASP.NET MVC Core 在ASP.NET Core上实现“Fire and Forget”方法的安全方法 - Safe way to implement a “Fire and Forget” method on ASP.NET Core C# ASP.NET Core 一劳永逸 - C# ASP.NET Core Fire and forget ASP.NET在每个请求上重新编译 - ASP.NET recompiles on every request 单元测试和多个参数传递给Asp.Net Core MVC中的构造函数,将令牌传递给每个请求 - Unit testing and multiple parameters to constructors in Asp.Net Core MVC, Passing token to every request 来自Javascript的每个http请求都会刷新Asp.net Core Controller - Asp.net Core Controller is refreshed in every http request from Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM