C# 在运行时更改OwinContext dbContext

C# 在运行时更改OwinContext dbContext,c#,asp.net-identity,owin,dbcontext,C#,Asp.net Identity,Owin,Dbcontext,我需要运行时更改Request.GetOwinContext()的dbContext以使用特定的连接字符串,但它不起作用 我有一个dbContex类来接受Web.Config中的默认连接字符串,或者像这样的指定连接: public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("AuthEntities

我需要运行时更改
Request.GetOwinContext()
dbContext
以使用特定的
连接字符串,但它不起作用

我有一个
dbContex
来接受
Web.Config
中的默认
连接字符串
,或者像这样的指定连接:

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;
    }

}
[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);

     (...)
}
public类ApplicationDbContext:IdentityDbContext
{
公共应用程序上下文()
:base(“AuthEntities”,throwifvv1schema:false)
{
Configuration.ProxyCreationEnabled=false;
Configuration.LazyLoadingEnabled=false;
}
公共应用程序DBContext(字符串sConnectionString)
:base(sConnectionString,throwifvv1schema:false)
{
Configuration.ProxyCreationEnabled=false;
Configuration.LazyLoadingEnabled=false;
}
公共静态应用程序上下文创建()
{
返回新的ApplicationDbContext();
}
公共静态应用程序上下文创建(字符串sConnectionString)
{
ApplicationDbContext测试=新的ApplicationDbContext(sConnectionString);
回归试验;
}
}
然后,当新用户注册时,我尝试将Owin的默认dbContext更改为具有新连接字符串的dbContext,如下所示:

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;
    }

}
[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);

     (...)
}
[AllowAnonymous]
[路线(“创建”)]
公共异步任务CreateUser(CreateUserBindingModel createUserModel)
{
(...)
//获取特定的connectionString
var connectionString=cService.GetCompanyConnectionString(createUserModel.Company);
//生成新的dbContext
var appDbContext=ApplicationDbContext.Create(connectionString);
//获取文本
var context=HttpContext.Current.GetOwinContext();
//替换OwinContext中的DbContext
Set(“ApplicationDbContext”,appDbContext);
(...)
}
直到并包括
appDbContext
所有内容都按预期工作,但在调试时,在设置
context.Set.
之后,
context
仍使用默认设置


我还尝试使用
var context=Request.GetOwinContext()
并直接设置为
HttpContext.Current.GetOwinContext().Set(“ApplicationDbContext”,appDbContext)
Request.GetOwinContext().Set(“ApplicationDbContext”,appDbContext)但我总是得到相同的结果。

这里的问题是我在设置时尝试使用的第一个字符串,所以这一行

context.Set<ApplicationDbContext>("ApplicationDbContext", appDbContext);
context.Set(“ApplicationDbContext”,appDbContext);
必须改为

context.Set<ApplicationDbContext>(appDbContext);
context.Set(appDbContext);

我相信这是因为
.Set
是一种弱类型方法?您好,我也这么做了,但usermanager仍在使用旧的dbcontext。您能在这里提供帮助吗: