0

Firstly I created choose database login page. After that login form appears in my project. In that logic, how to change connection with database in SignInManager and UserManager?

In Startup.cs I have this code:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ABCDBContext>(options => 
         options.UseMySql(Configuration.GetConnectionString("ABCDBConnection")));
}

In the controller:

public AccountController(UserManager<ApplicationUser> userManager,
                         SignInManager<ApplicationUser> signInManager,
                         ILogger<AccountController> logger,
                         EmailSender emailSender, IUnitOfWork unitOfWork, 
                         IStringLocalizer<Resource> localizer,
                         IWebHostEnvironment webHostEnvironment, 
                         IConfiguration config)
{
    _config = config;
    _userManager = userManager;
    _signInManager = signInManager;
    _logger = logger;
    _emailSender = emailSender;
    _unitOfWork = unitOfWork;
    _localizer = localizer;
    _webHostEnvironment = webHostEnvironment;

    System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
    urlPath = _config.GetValue<string>("ImagePath:UrlPath");            
}

In DBContext:

public class ABCDBContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid, IdentityUserClaim<Guid>, ApplicationUserRole, IdentityUserLogin<Guid>, IdentityRoleClaim<Guid>, IdentityUserToken<Guid>>
{   
    public ABCDBContext(DbContextOptions<ABCDBContext> options)
        : base(options)
    {
    } 
}

In appsettings.json:

"ConnectionStrings": {
    "DBConnection": "Server=localhost;Database=testingv7;user=root;password=123@abc",
    "Testing20230220": "Server=localhost;Database=testingv8;user=root;password=123@abc",
    "Testing20230221": "Server=localhost;Database=testingv9;user=root;password=123@abc",
    "Testing20230222": "Server=localhost;Database=testingv10;user=root;password=123@abc",
    "Testing20230223": "Server=localhost;Database=testingv11;user=root;password=123@abc",
    "Testing20230224": "Server=localhost;Database=testingv12;user=root;password=123@abc"
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Cyrus
  • 23
  • 1
  • 11
  • Should every user login change to connection string or do you want to have a different connection string per user? – Michal Diviš Feb 23 '23 at 06:08
  • Yes, I have a different connection string per user. – Cyrus Feb 23 '23 at 06:41
  • 1
    Hi @Cyrus, have you checked [this answer](https://stackoverflow.com/a/69248688/11398810)? – Rena Feb 23 '23 at 06:58
  • @Cyrus You can't re-configure the context in the `AddDbContext` method as that only happens once at the start of the app. What you could do is simply create a new instance of the DB context with the required connection string for each user request. – Michal Diviš Feb 23 '23 at 12:05

1 Answers1

0

You can't re-configure the context in the AddDbContext method as that only happens once at the start of the app.

What you could do is simply create a new instance of the DB context with the required connection string for each user request. Sure, this approach doesn't register the DB context into the DI container, but with the way you want to use it, it doesn't really make sense to use DI (IMO).

public class AppDbContext : DbContext 
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {
    }
}

public static class AppDbContextFactory
{
    public static AppDbContext Create(string connectionString)
    {
        var options = new DbContextOptionsBuilder<AppDbContext>()
            .UseMySql(connectionString)
            .Options;
        return new AppDbContext(options);
    }
}

public class AccountController
{
    public AccountController(...)
    {
        ...
    }

    private void WhenContextIsNeededForUser(string userSpecificConnectionString)
    {
        //get a DB context specific to the user
        var dbContext = AppDbContextFactory.Create(userSpecificConnectionString);

        //do stuff
        dbContext.Set<Product>().Add(new Product());
        dbContext.SaveChanges();
    }
}
Michal Diviš
  • 2,008
  • 12
  • 19