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

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

问题描述

我想在控制器中更改 sql 连接字符串,而不是在 ApplicationDbContext 中.我正在使用 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 方法中使用IServiceCollectionimplementationfactory 重载,像这样:

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.

更新 #1Yuriy 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 中的源代码,所以我查找了 AddDbContext 确实如此.嗯……这并不难.这些添加"(和使用")扩展方法只不过是方便的方法,请记住这一点.因此,您需要添加 AddDbContext 所做的所有服务以及选项.也许您甚至可以重用 AddDbContext 扩展方法,只需使用实现工厂添加您自己的重载即可.

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天全站免登陆