简体   繁体   中英

.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). 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?

2. Is it possible to configure(Dependency Injection) DbContext after launching the application?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM