I have a system with a main database, where all users log in and get the data to connect to their database, generate the connection string, how do I change the connection string of the context responsible for the database user?
I need to do this inside a controller responsible for logging in the user
public async Task<IActionResult> Login([FromBody] AuthenticateRequest model)
{
try
{
var response = await _userService.Authenticate(model); //conection in ContextPrincipal principal
string strConn = response.strConnection;
//change connection string ContextUsers here
var dataMyBD = await _myBdRepository.getUsers(model); //conection in ContextUsers users database`s
}
catch (Exception)
{
return null;
}
}
it could also be a select with several connection strings, the base structure is the same, you can use the same context, I just need to change the connection string.
mgebhard
The connection string is passed as an option. See the startup.cs file.
I do this, but I need to change the connection string after that, after the user logs in, he will connect to another database.
var connectionPrincipal = Configuration.GetConnectionString("connectionPrincipal");
services.AddDbContext<OmnisSYSContext>(options => options.UseMySql(connectionPrincipal));
var connectionUsers = Configuration.GetConnectionString("connectionUsers");
services.AddDbContext<OmnisUSERContext>(options => options.UseMySql(connectionUsers)); //NEED CHANGE THIS AT RUN TIME
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Can you clarify your expectations and application requirements? Is this a multi-tenant application? Do all the databases have the same schema?
A user must login to identify the application user. The design needs al least one database that contains user accounts. The same database can contain whatever user information is required like a connection string.
Look into registering a DB Factory to generate a DB context using standard DI.
None
0 Points
11 Posts
asp.net core Entity Framework changing connection string at run time
Dec 01, 2020 08:21 PM|DarkDucke|LINK
I have a system with a main database, where all users log in and get the data to connect to their database, generate the connection string, how do I change the connection string of the context responsible for the database user?
I need to do this inside a controller responsible for logging in the user
I found the post "https://stackoverflow.com/questions/36816215/dynamically-change-connection-string-in-asp-net-core" but no option worked for me
how can I do this?
All-Star
52101 Points
23232 Posts
Re: asp.net core Entity Framework changing connection string at run time
Dec 01, 2020 08:24 PM|mgebhard|LINK
The connection string is passed as an option. See the startup.cs file.
None
0 Points
11 Posts
Re: asp.net core Entity Framework changing connection string at run time
Dec 01, 2020 09:14 PM|DarkDucke|LINK
it could also be a select with several connection strings, the base structure is the same, you can use the same context, I just need to change the connection string.
I do this, but I need to change the connection string after that, after the user logs in, he will connect to another database.
var connectionPrincipal = Configuration.GetConnectionString("connectionPrincipal");
services.AddDbContext<OmnisSYSContext>(options => options.UseMySql(connectionPrincipal));
var connectionUsers = Configuration.GetConnectionString("connectionUsers");
services.AddDbContext<OmnisUSERContext>(options => options.UseMySql(connectionUsers)); //NEED CHANGE THIS AT RUN TIME
Member
730 Points
246 Posts
Re: asp.net core Entity Framework changing connection string at run time
Dec 02, 2020 03:10 AM|Jerry Cai|LINK
Hi,DarkDucke
Do you mean create two databases and then choose which one to use depends on whether the user has login?
This requirement can be fulfilled, first you should check both database is created, then use a parameter to judge whether the user has login. If
logined, you can get the data from another database, like:
Best Regards,
Jerry Cai
All-Star
52101 Points
23232 Posts
Re: asp.net core Entity Framework changing connection string at run time
Dec 02, 2020 11:54 AM|mgebhard|LINK
Can you clarify your expectations and application requirements? Is this a multi-tenant application? Do all the databases have the same schema?
A user must login to identify the application user. The design needs al least one database that contains user accounts. The same database can contain whatever user information is required like a connection string.
Look into registering a DB Factory to generate a DB context using standard DI.
https://docs.microsoft.com/en-us/ef/core/cli/dbcontext-creation?tabs=dotnet-core-cli
https://docs.microsoft.com/en-us/ef/core/dbcontext-configuration/