在运行时更改 OwinContext dbContext
change OwinContext dbContext on runtime
我需要 运行-time 更改 Request.GetOwinContext()
的 dbContext
以使用特定的 connectionString
但它不起作用。
我有一个 dbContex
class
接受来自 Web.Config
的默认 connectionString
或像这样的指定的:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("AuthEntities", throwIfV1Schema: false)
{
Configuration.ProxyCreationEnabled = false;
Configuration.LazyLoadingEnabled = false;
}
public ApplicationDbContext(string sConnectionString)
: base(sConnectionString, throwIfV1Schema: false)
{
Configuration.ProxyCreationEnabled = false;
Configuration.LazyLoadingEnabled = false;
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public static ApplicationDbContext Create(string sConnectionString)
{
ApplicationDbContext test = new ApplicationDbContext(sConnectionString);
return test;
}
}
然后,当新用户注册时,我尝试使用新的 connectionString 更改 Owin 的默认 dbContext,如下所示:
[AllowAnonymous]
[Route("create")]
public async Task<IHttpActionResult> CreateUser(CreateUserBindingModel createUserModel)
{
(...)
//get the specific connectionString
var connectionString = cService.GetCompanyConnectionString(createUserModel.Company);
//generate new dbContext
var appDbContext = ApplicationDbContext.Create(connectionString);
//get OwinContext
var context = HttpContext.Current.GetOwinContext();
//replace the DbContext inside OwinContext
context.Set<ApplicationDbContext>("ApplicationDbContext", appDbContext);
(...)
}
直到并包括 appDbContext
一切都按预期工作,但在调试时,设置 context.Set<ApplicationDbContext>...
后 context
仍在使用默认值。
我也尝试过使用 var context = Request.GetOwinContext();
并直接设置为 HttpContext.Current.GetOwinContext().Set<ApplicationDbContext>("ApplicationDbContext", appDbContext);
和 Request.GetOwinContext().Set<ApplicationDbContext>("ApplicationDbContext", appDbContext);
但我总是得到相同的结果。
这里的问题是我在设置时尝试使用的第一个字符串,所以这一行
context.Set<ApplicationDbContext>("ApplicationDbContext", appDbContext);
必须改为
context.Set<ApplicationDbContext>(appDbContext);
我需要 运行-time 更改 Request.GetOwinContext()
的 dbContext
以使用特定的 connectionString
但它不起作用。
我有一个 dbContex
class
接受来自 Web.Config
的默认 connectionString
或像这样的指定的:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("AuthEntities", throwIfV1Schema: false)
{
Configuration.ProxyCreationEnabled = false;
Configuration.LazyLoadingEnabled = false;
}
public ApplicationDbContext(string sConnectionString)
: base(sConnectionString, throwIfV1Schema: false)
{
Configuration.ProxyCreationEnabled = false;
Configuration.LazyLoadingEnabled = false;
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public static ApplicationDbContext Create(string sConnectionString)
{
ApplicationDbContext test = new ApplicationDbContext(sConnectionString);
return test;
}
}
然后,当新用户注册时,我尝试使用新的 connectionString 更改 Owin 的默认 dbContext,如下所示:
[AllowAnonymous]
[Route("create")]
public async Task<IHttpActionResult> CreateUser(CreateUserBindingModel createUserModel)
{
(...)
//get the specific connectionString
var connectionString = cService.GetCompanyConnectionString(createUserModel.Company);
//generate new dbContext
var appDbContext = ApplicationDbContext.Create(connectionString);
//get OwinContext
var context = HttpContext.Current.GetOwinContext();
//replace the DbContext inside OwinContext
context.Set<ApplicationDbContext>("ApplicationDbContext", appDbContext);
(...)
}
直到并包括 appDbContext
一切都按预期工作,但在调试时,设置 context.Set<ApplicationDbContext>...
后 context
仍在使用默认值。
我也尝试过使用 var context = Request.GetOwinContext();
并直接设置为 HttpContext.Current.GetOwinContext().Set<ApplicationDbContext>("ApplicationDbContext", appDbContext);
和 Request.GetOwinContext().Set<ApplicationDbContext>("ApplicationDbContext", appDbContext);
但我总是得到相同的结果。
这里的问题是我在设置时尝试使用的第一个字符串,所以这一行
context.Set<ApplicationDbContext>("ApplicationDbContext", appDbContext);
必须改为
context.Set<ApplicationDbContext>(appDbContext);