简体   繁体   English

.NET Core 2.2 Web应用程序。 DbContext是通过依赖注入设置的,可以在运行时更改它吗?

[英].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.

相关问题 依赖注入网络核心控制台应用程序设置 - Dependency injection net core console application setup 如何在.net Core 2.2控制台应用程序中实现依赖项注入 - How to implement dependency injection in .net core 2.2 console application .NET Core 2.1 DbContext ObjectDisposedException 依赖注入 - .NET Core 2.1 DbContext ObjectDisposedException Dependency Injection 在 .NET 核心控制台应用程序中为 EF Core DbContext 服务错误建立依赖注入 - Established dependency injection in .NET Core Console Application for EF Core DbContext Service Error 具有依赖项注入的 EF 核心 DbContext - EF core DbContext with dependency injection 将连接字符串添加到DBContext(NpgSQL,依赖注入,.NET Core) - Adding connection string to DBContext (NpgSQL, Dependency Injection, .NET Core) ASP.NET Core-依赖注入-IdentityServer4 DbContext - ASP.NET Core - Dependency Injection - IdentityServer4 DbContext .NET 核心依赖注入 - 许多数据库,一个 DbContext - .NET Core dependency injection - many databases, one DbContext Net Core:使用Xunit为工作单元模式和DBContext执行依赖注入 - Net Core: Execute Dependency Injection with Xunit for Unit of Work Pattern and DBContext 如何使用.Net Core 2.1配置依赖项注入的DbContext? - How do I configure a DbContext with dependency injection with .Net Core 2.1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM