2017-07-27 59 views
0

我想根据用户在登录屏幕输入的代码在运行时更改我的连接字符串。我做了以下Owin ApplicationDbContext没有更新

ApplicationDbContext

public static ApplicationDbContext Create(string scCode){ 
     return new ApplicationDbContext("name=GEContext_" + scCode); 
    } 

而且在登录时我改变的ConnectionString如下

public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
    { 
     if (ModelState.IsValid) 
     { 
      try 
      { 
       System.Web.HttpContext.Current.Session["SchoolCode"] = model.SchoolCode; 

       var appDbContext = ApplicationDbContext.Create(model.SchoolCode); 
       HttpContext.GetOwinContext().Set<ApplicationDbContext>(appDbContext); 
.... 
    } 
} 
} 

现在它仍然是指原始数据库......我失去了什么?

P.S.对于历史/详细考虑this post

回答

0

您好我得到了here

应答的问题是在ApplicationDbContext我们需要在我的情况来指定一个默认的数据库,而该默认数据库必须改变。所以我改变它使用

var appDbContext = ApplicationDbContext.Create(System.Web.HttpContext.Current.Session["SchoolCode"].ToString());//new ApplicationDbContext("name=GEContext", System.Web.HttpContext.Current.Session["SchoolCode"].ToString()); 

HttpContext.GetOwinContext().Set<ApplicationDbContext>(appDbContext); 
HttpContext.GetOwinContext().Set<ApplicationUserManager>(new ApplicationUserManager(new UserStore<ApplicationUser, Role, int, UserLogin, UserRole, UserClaim>(appDbContext))); 

return HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();