在运行时更改OwinContext dbContext [英] change OwinContext dbContext on runtime

查看:183
本文介绍了在运行时更改OwinContext dbContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在运行时将Request.GetOwinContext()dbContext更改为使用特定的connectionString,但是它不起作用.

I need to run-time change the dbContext for Request.GetOwinContext() to use a specific connectionString but it's not working.

我有一个dbContex class可以接受Web.Config中的默认connectionString或类似这样的指定值:

I've a dbContex class to accept a default connectionString from Web.Config or a specified one like this:

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,如下所示:

then when a new user is registering i've tried to change the Owin's default dbContext for one with a new connectionString like this:

[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仍使用默认值.

until and including appDbContext everything works as expected but on debuging, after setting context.Set<ApplicationDbContext>... the context is still using the default one.

我也尝试过使用var context = Request.GetOwinContext();并将其直接设置为HttpContext.Current.GetOwinContext().Set<ApplicationDbContext>("ApplicationDbContext", appDbContext);Request.GetOwinContext().Set<ApplicationDbContext>("ApplicationDbContext", appDbContext);,但是我总是得到相同的结果.

I've also tried using var context = Request.GetOwinContext(); and setting directly as HttpContext.Current.GetOwinContext().Set<ApplicationDbContext>("ApplicationDbContext", appDbContext); and Request.GetOwinContext().Set<ApplicationDbContext>("ApplicationDbContext", appDbContext); but I always get the same result.

推荐答案

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

the problem here was the first string I was trying to use when setting, so this line

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

必须更改为

context.Set<ApplicationDbContext>(appDbContext);

这篇关于在运行时更改OwinContext dbContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆