I'm trying to implement multi-database structure with automatic migrations. databases uncountable and i can't set the a fixed connection strings. i tried many ways to handle it, some ways worked but could not handle automatic migrations. I have two different DbContexts and different connection strings the question is: Is this a good way to handle it or there is a better one ?
public class CategoriesController : Controller
{
private readonly UserDbContext _context;
public CategoriesController(UserDbContext context, ApplicationDbContext _Maincontext)
{
var conn = _Maincontext.Users.FirstOrDefault().DbId;
context.Database.SetConnectionString($"Data Source=.\\SQLEXPRESS;Initial Catalog={conn};Integrated Security=False; uid=sa;password=123;");
context.Database.Migrate();
_context = context;
}
// GET: Categories
public async Task<IActionResult> Index()
{
return Ok(await _context.Categories.ToListAsync());
}
}