locked
asp.net core Entity Framework changing connection string at run time RRS feed

  • Question

  • User324714158 posted

    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;
                }
            }
    

    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?

    Tuesday, December 1, 2020 8:21 PM

All replies

  • User475983607 posted

    The connection string is passed as an option.  See the startup.cs file.

    Tuesday, December 1, 2020 8:24 PM
  • User324714158 posted

    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

    Tuesday, December 1, 2020 9:14 PM
  • User1312693872 posted

    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:

     if (choose == 1)
                {
                    var model = _context.Students.ToList();
                    return View(model);
                }
                else
                {
                    var model = _context1.Students.ToList();
                    return View(model);
                }
    services.AddDbContext<MyDbContext>(options =>
          options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    services.AddDbContext<MyDbContext1>(options =>
          options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection1")));

    Best Regards,

    Jerry Cai

    Wednesday, December 2, 2020 3:10 AM
  • User475983607 posted

    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/

    Wednesday, December 2, 2020 11:54 AM