I need help to implement in the controller when user call an API like ChangeDatabase(string databaseConexionString), then all the request made for that user will use the new connection string instead.
[Produces("application/json")]
[Route("api/admin")]
public class AdminController : Controller
{
private readonly DatabaseContext _context;
private readonly IMapper _mapper;
public AdminController(DatabaseContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
[HttpGet("[action]")]
public IActionResult ChangeDatabase(string databaseConexionString)
{
//change conexion string...
return Ok("changed!");
}
}
My startup.cs file, the configure services is the default
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DatabaseContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
I just want to change between databases and keep the same context class because the tables are the same, the only difference is the data.
1 Answer(s)
-
0
Hi,
You can implement a custom connection string resolver (<a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/dev/src/Abp/Domain/Uow/IConnectionStringResolver.cs">https://github.com/aspnetboilerplate/as ... esolver.cs</a>) and return the correct connection string.
If user changes connection string using the UI, you can store it in a setting and return the setting value in your custom connection string resolver. If there is no setting value for the user, you can return the default connection string.