How to change database name in dbcontext connection string at runtime

后端 未结 3 2212
孤独总比滥情好
孤独总比滥情好 2020-12-06 23:34

My Asp.Net MVC application is setup as follows. There are 4 projects in solution.

  • ge.Web
  • ge.BLL
  • ge.Core
  • ge.Entities

<

3条回答
  •  攒了一身酷
    2020-12-07 00:12

    I resolved it with the help of itikhomi..Posting the final code..

    ApplicationDbContext

    public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext("name=GEContext");
        }
    

    AccountController

     public ApplicationUserManager UserManager {
            get
            {
                if (System.Web.HttpContext.Current.Session["SchoolCode"] == null)
                    return _userManager ?? HttpContext.GetOwinContext().GetUserManager();
                else
                {
                    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(appDbContext);
                    HttpContext.GetOwinContext().Set(new ApplicationUserManager(new UserStore(appDbContext)));
    
                    return HttpContext.GetOwinContext().GetUserManager();
                }
            }
            private set
            {
                _userManager = value;
            }
        }
    

提交回复
热议问题