In a Net Core 5 Web API application, I need to change the connection string of a secondary dbcontext with information obtained from the main dbcontext
There are two dbcontext:
DbContextMaster - static connection string
DbContextChild - dynamic connection string. Use the template in appsettings like this:
"DbContextChild": "Data Source=server;Initial Catalog={dbName};Integrated Security=False;User Id=username;Password="
When starting the application:
I get a header parameter to identify the tenant (so far ok)
I need to perform a query with this parameter using a DbContextMaster to get the database name. If the value of the header parameter identifying the tenant DOES NOT exist in this database, it must return badRequest, if it exists, must change DbContextChild template with database name from tenant, so that only then does the controller getting the data from the DbContext Child respond to the requests.
I have many questions about how to carry out such an implementation. Is there any way to intercept the call from all controllers by doing the above checks? Or would it be necessary to have a base controller and everyone else inherit it?