ConnectionString can be changed in OnConfiguring method of DbContext as below:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//get connectionString
var connString = _connectionStringService.GetConnectionString();
optionsBuilder.UseSqlServer(connString);//or whatever
}
And also, in order to fetch custom connectionString, you can have some service injected to be used in OnConfiguring method:
public MyDbContext(IConnectionStringService connectionStringService)
: base(options)
{
_connectionStringService = connectionStringService) ;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…