动态更改Asp.Net Core中的连接字符串 [英] Dynamically change connection string in Asp.Net Core

查看:497
本文介绍了动态更改Asp.Net Core中的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在控制器中而不是在ApplicationDbContext中更改sql连接字符串.我正在使用Asp.Net Core和Entity Framework Core.

I want to change sql connection string in controller, not in ApplicationDbContext. I'm using Asp.Net Core and Entity Framework Core.

例如:

public class MyController : Controller {
    private readonly ApplicationDbContext _dbContext
    public MyController(ApplicationDbContext dbContext)
    {
        _dbContext = dbContext;
    }
    private void ChangeConnectionString()
    {
    // So, what should be here?
    } }

我该怎么做?

推荐答案

我们有一个与您相似的案例.我们要做的是在 Startup 类的 ConfigureServices 方法中使用 IServiceCollection implementationfactory 重载,像这样:

We have a case similar to you. What we've done is use the implementationfactory overload of the IServiceCollection in the ConfigureServices method of the Startup class, like so:

//First register a custom made db context provider
services.AddTransient<ApplicationDbContextFactory>();
//Then use implementation factory to get the one you need
services.AddTransient(provider => provider.GetService<ApplicationDbContextFactory>().CreateApplicationDbContext());

对于我来说,现在为您实现CreateApplicationDbContext非常困难,因为它完全取决于您的确切需求.但是一旦您确定了该部分的精确度后,该方法的基础无论如何应该看起来像这样:

It is very difficult for me right now to implement CreateApplicationDbContext for you, because it totally depends on what you want exactly. But once you've figured that part out how you want to do it exactly, the basics of the method should look like this anyway:

public ApplicationDbContext CreateApplicationDbContext(){
  //TODO Something clever to create correct ApplicationDbContext with ConnectionString you need.
} 

一旦实现,您就可以像在构造函数中一样在控制器中注入正确的ApplicationDbContext:

Once this is implemented you can inject the correct ApplicationDbContext in your controller like you did in the constructor:

public MyController(ApplicationDbContext dbContext)
{
    _dbContext = dbContext;
}

或控制器中的操作方法:

Or an action method in the controller:

public IActionResult([FromServices] ApplicationDbContext dbContext){
}

无论如何实现细节,诀窍在于实现工厂每次注入时都会构建ApplicationDbContext.

However you implement the details, the trick is that the implementation factory will build your ApplicationDbContext everytime you inject it.

如果需要更多帮助实施此解决方案,请告诉我.

Tell me if you need more help implementing this solution.

更新#1 Yuriy N.询问了AddTransient和AddDbContext有什么区别,这是一个有效的问题,但事实并非如此.让我解释一下.

Update #1 Yuriy N. asked what's the difference between AddTransient and AddDbContext, which is a valid question... And it isn't. Let me explain.

这与原始问题无关.

但是...话虽如此,在这种情况下,使用实体框架来实现自己的实现工厂"(这是我的回答中最重要的一点)可能比我们需要的要复杂得多.

BUT... Having said that, implementing your own 'implementation factory' (which is the most important thing to note about my answer) can in this case with entity framework be a bit more tricky than what we needed.

但是,现在有了这样的问题,我们现在可以幸运地查看GitHub中的源代码,因此我查找了

However, with questions like these we can nowadays luckily look at the sourcecode in GitHub, so I looked up what AddDbContext does exactly. And well... That is not really difficult. These 'add' (and 'use') extension methods are nothing more than convenience methods, remember that. So you need to add all the services that AddDbContext does, plus the options. Maybe you can even reuse AddDbContext extension method, just add your own overload with an implementation factory.

那么,回到您的问题. AddDbContext做一些EF特定的东西.如您所见,它们将允许您在以后的版本(瞬态,单例)中度过一生. AddTransient是Asp.Net Core,可让您添加所需的任何服务.您需要一个实现工厂.

So, to come back to your question. AddDbContext does some EF specific stuff. As you can see they are going to allow you to pass a lifetime in a later release (transient, singleton). AddTransient is Asp.Net Core which allows you to add any service you need. And you need an implementation factory.

这是否更清楚?

这篇关于动态更改Asp.Net Core中的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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