动态更改Asp.Net Core中的连接字符串

编程入门 行业动态 更新时间:2024-10-21 10:14:48
本文介绍了动态更改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中的源代码,因此我查找了 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中的连接字符串

本文发布于:2023-11-14 19:35:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1588337.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   动态   Net   Asp   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!