[英]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: 例如:
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.