I wanted to check in program.cs "force" after "builde", when the application is loaded, if it is local (127.0.0.1), it will use the local database (LocalConnection), and if it is a server, it will use the server database(DefaultConnection).
(It is not useful for me after app and only after build needed)
var builder = WebApplication.CreateBuilder(args);
var connectionString="";
bool isLocal= ...?<<<<<<<<<<<<?
if (isLocal)
{
var connectionString0 = builder.Configuration.GetConnectionString("LocalConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
connectionString = connectionString0;
}
else
{
var connectionString0 = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
connectionString = connectionString0;
}
...
var app = builder.Build();
//It is not useful for me after app.
...
pls befor answer that, check your solution in blazor web app 8.0 only, tnks.
not working:
If your want different ConnectionString depending on the some information in the request (such as "hostname" to determine if the request from local) , you could register two DbContext( such as "MyContext1" and "MyContext2") with different ConnectionString. Then Create an Interface for them such as "IMyContext", the following code will let the IMyContext resolve different service depending on request.