EFCore和.net Core2.0中与DbContext的多重连接 [英] Multiple connection to DbContext in EFCore and .net Core2.0

查看:158
本文介绍了EFCore和.net Core2.0中与DbContext的多重连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用多重连接连接到DBcontext时遇到了问题.请帮我解决这个问题,这是我的情况.

I have facing a problem to connecting to DBcontext with multiple connection. please help me out from this problem , here is me scenario .

  1. 我有一个Angular5应用程序和Webapi控制器.我有4个数据库,每个数据库都有不同的用户.

  1. I have an Angular5 application and Webapi Controllers. I have 4 databases and Each database has different users .

当4个用户试图通过Webapi控制器同时连接到其数据库时,此时所有用户都从单个数据库获取数据(而不是获取其他数据库).

When 4 user are trying to connecting to their database at Same time through Webapi controller , that time all users are getting data from single database (instead of getting different database ) .

当我们尝试一一连接时,数据将正确获取.

When we try to Connecting one by one then data getting correctly .

这是我用于动态连接的示例代码.

Here is my sample code for dynamic connection .

上下文连接字符串:

 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
         #warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
          optionsBuilder.UseSqlServer(LoginController.ConnectionString);

    }

Controler API:

Controler API :

public IEnumerable<object> Getvendordetails([FromBody] dynamic id)
    {
        if (Request != null)
        {
            LoginController.DynamicDbname = Request.Headers["Connectionstring"];
        }
        ContextEntity obj = new ContextEntity();
    }

此处 LoginController.DynamicDbname 是静态变量.

编辑代码:

控制器:

    namespace AngularDotnetCore.Controllers
   {
     [Route("api/[controller]")]
    public class HomeController : Controller
    {
       private readonly IHttpContextAccessor httpContext;
       private ContextEntity db;
      public HomeController(IHttpContextAccessor _httpContext)
    {

        this.httpContext = _httpContext;
        db = new ContextEntity(httpContext);

    }
    [HttpPost("GetVendors")]
    public IEnumerable<object> Getvendordetails([FromBody] dynamic id)
    {
        //string DynamicConnection = Request.Headers["Connection"];
        db = new ContextEntity(httpContext);
        return db.Vendors;
    }

    [HttpGet("GetItems")]
    public IEnumerable<object> GetItems()
    {
        //string DynamicConnection = Request.Headers["Connection"];
        db = new ContextEntity(httpContext);
        return db.Items;
    }

}

}

DBContext:

   private readonly HttpContext httpContext;
  public ContextEntity(IHttpContextAccessor httpContextAccessor)
    : base()
    {
        httpContext = httpContextAccessor.HttpContext;
    }
     protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
   {
       var connection = httpContext.Request.Headers["Connection"];
      optionsBuilder.UseSqlServer(connection);

   }

对于每个API请求,我都已将httpcontext参数传递给Dbcontext基类.

I have pass httpcontext parameter to Dbcontext base class for each API Request.

请在这个问题上帮助我,如果我的代码错误,请以很好的方式建议我.

Please help me on this problem , if my code is wrong please suggest me in good way.

谢谢 维克多·A

推荐答案

要从DbContext访问http标头,可以尝试IHttpContextAccessor,无需引用LoginContoller.

For accessing http header from DbContext, you could try IHttpContextAccessor, there is no need to refer LoginContoller.

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    private readonly HttpContext httpContext;
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, IHttpContextAccessor httpContextAccessor)
        : base(options)
    {
        httpContext = httpContextAccessor.HttpContext;
    }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var connection = httpContext.Request.Headers["Connectionstring"];
        optionsBuilder.UseSqlServer(connection);
    }       
}

编辑似乎您在控制器中初始化了ContextEntity而不是使用DI,如果这样,请尝试直接传递连接字符串而不是IHttpContextAccessor.

Edit It seems you initlize the ContextEntity in the controller instead of using DI, if so, try passing the connectionstring directly instead of IHttpContextAccessor.

namespace AngularDotnetCore.Controllers
   {
 [Route("api/[controller]")]
public class HomeController : Controller
{      
   private ContextEntity db;
  public HomeController()
{

    var connection = Request.Headers["Connection"];
    db = new ContextEntity(connection);

}
  }

DbContext

private readonly string _connection;
  public ContextEntity(string connection)
: base()
{
    _connection = connection;
}
 protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
   {
  optionsBuilder.UseSqlServer(_connection);

    }

这篇关于EFCore和.net Core2.0中与DbContext的多重连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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