2017-07-27 49 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>();