2

In ConfigureServices I call

 public void ConfigureServices(IServiceCollection services){
                   services.AddDbContext<MyDbContext>(
                    options => options.UseSqlServer(
                        _configuration.GetConnectionString(connectionString)));
}

but now I want a second instance of MyDbContext that uses different connection string. How to correctly do it so dependency still works? Is the only way is to create an empty MyDbContext subclass and register it this way?

Yoda
  • 17,363
  • 67
  • 204
  • 344
  • Does this answer your question? [Entity Framework Core multiple connection strings on same DBContext?](https://stackoverflow.com/questions/42616408/entity-framework-core-multiple-connection-strings-on-same-dbcontext) – Steve Norwood Dec 15 '21 at 08:25
  • @SteveNorwood He does, what I proposed creating dummy empty subclass. Is there another way? – Yoda Dec 15 '21 at 08:50
  • Not that I'm aware of. Dependency Injection will need some way of working out the difference between your two database connections, and this is the simplest way that I can see. I know with HttpClients you can give them names (https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-6.0#named-clients) but this is quite different and I can't see anything like this for EF – Steve Norwood Dec 15 '21 at 12:39

0 Answers0