ASP.NET Core OnModelCreating force to fire in every request
I have ASP.NET Core (1.1.0) and SQL Server database. In login page I have a form with a select box with name of companies.
For example:
- Company 1
- Company 2
If a user selects the the first "Company 1", I need to rename all the tables with a company prefix:
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.
Is it possible to force OnModelCreating for every request?
Or change table name after dbContext initilization?
Thanks!
ActionFilter
in theFilterConfig
– Ryan Searle Jan 27 '17 at 10:15