I'm developing an application in .NET Core 3.1 and EF Core based on multiple tenants that will have a separate database for each tenant and also a unique database called accounts that has tenants' account data, including the string of connection (databasename), users and other things. I have two schemes, one for master (which is for accounts) and tenant (which is for tenants, being identical for them). Tenants will access the same app.myapp.com URL and at the moment I cannot have a subdomain for each of them.
I set my connection string in the Startup.cs class:
services.AddDbContext<MasterContext>(options => options.UseMySql(configuration.GetConnectionString("MasterConnection")));
services.AddDbContext<TenantContext>(options => options.UseMySql(configuration.GetConnectionString("TenantConnection")));
My appsettings.json:
{
"ConnectionStrings": {
"MasterConnection": "host=localhost;port=3306;user id=root;password=MYPASSWORD;database=app_master;",
"TenantConnection": "host=localhost;port=3306;user id=root;password=MYPASSWORD;database=app_tenant;"
}
}
Users have the reference of which account he is part of, so when they log in, I will know which account he belongs to, which connection string and databasename.
The difficulty is changing the connection string after login to TenantContext. How should I proceed?
Thank you very much in advance.