简体   繁体   中英

How to pass the connection name as a parameter to the constructor for IdentityDbContext

I like to give the the users of my web application to choose the database to which to log in. Like in:

public class MyDbContext : IdentityDbContext<MyUser>
{
  public MyDbContext() : base("TheNameOfTheConnectionString")
  {
  }
}

I would like to replace the fixed string "TheNameOfTheConnectionString" with a parameter based on the choice the user made. I tried through session variable, but the session is not created when this constructor is called, and I have no idea how to pass a value which the user have chosen. Can anybody help?

You can add a parameter to the MyDbContext constructor:

public class MyDbContext : IdentityDbContext<MyUser>
{
   public MyDbContext(string connectionString) : base(connectionString)
   {
   }
}

Once you have the user's choice, you can send it as parameter to the MyDbContext while creating an instance of it, which will in turn tell the base class, which connection string it should use

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM