Having my own implementation of the IdentityDbContext, I would like it to be able to connect to a custom database according to the user's choice. So I have created 2 constructors for both default database and user selected database.
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("Users", throwIfV1Schema: false)
{
//Aici am adaugat
Configuration.ProxyCreationEnabled = true;
Configuration.LazyLoadingEnabled = true;
}
public ApplicationDbContext(String connectionName)
: base(connectionName, throwIfV1Schema: false)
{
//Aici am adaugat
Configuration.ProxyCreationEnabled = true;
Configuration.LazyLoadingEnabled = true;
}
}
My problem is now the way I would bring the custom connectionName
into the class.
The constructor is called in this method:
public static string conn;
public static ApplicationDbContext Create()
{
if(conn == null)
return new ApplicationDbContext();
else
return new ApplicationDbContext(conn);
}
Using session variables is impossible because in this context HttpContext.Current
is null. Adding a string argument to the Create
method is also impossible, because right in the Startup
class, before any user selection, Owin decides on using a default database:
app.CreatePerOwinContext(() => ApplicationDbContext.Create());
Even passing an argument there would not help because it wouldn't have been chosen by the user.
What can I do about it?
Thank you very much!