[英].NET Core 2.2 Web Application. DbContext was setup by Dependency Injection Can it be changed at Runtime?
I'm trying to work with several databases in which have the same schema structure with my .NET Core 2.2 Web application (with Dependency Injection approach + Entity Framework). 我正在尝试使用.NET Core 2.2 Web应用程序(具有依赖注入方法+实体框架)的几个具有相同架构结构的数据库。 I want to allow user can choose (or change) the database at runtime.
我想允许用户可以在运行时选择(或更改)数据库。 so I have 2 questions.
所以我有两个问题。
1. Is it possible to change DbContext
during the runtime? 1.是否可以在运行时更改
DbContext
?
2. Is it possible to configure(Dependency Injection) DbContext
after launching the application? 2.启动应用程序后是否可以配置(依赖注入)
DbContext
?
If so, How? 如果是这样,如何?
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
...
services.AddDbContext<MyDbContext>(option => option.UseMySql(Configuration.GetConnectionString("DevConnection")));
// Can I declare this after launching the app? or Can it be changed at Runtime?
...
I figured out. 我想通了。 and also shared in other thread.
并在其他线程中共享。 Please check, If someone interested
请检查,如果有人感兴趣
https://stackoverflow.com/a/57064965/4735043 https://stackoverflow.com/a/57064965/4735043
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.