You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Multiple instances of the same DbContext with different connection strings #9616
My ASP.NET Core 2.0 application must support multiple databases. Which database is to be used should depend on the ApplicationUser issuing a request on a per-request basis.
That rules out AddDbContext in Startup.cs with a single connection string (there will be multiple connection strings in my application.json file).
Note: I don't need multiple instances of the same context in my controller, as discussed here aspnet/DependencyInjection#352
I just need the correct one depending on the user.
In my SO post 'poke' comments:
Database contexts are usually configured to be created once per every request, so the configuration can change on a per-request basis. You just need to come up with a nice way to configure the contexts depending on your user ... So you should use a single database context but just make sure that your DbContextOptions configure the correct connection string per request. Or put the logic inside the db context (ugh though) by overriding OnConfiguring
@nicolasr75 Assuming you are using EF Core 2.0, then you can put logic into the code that builds the options. For example:
services.AddDbContext<MyDbContext>((serviceProvider,builder)=>{// Use some code here to look up the connection string// Note that the service provider (serviceProvider) is available if neededvarconnectionString=...;builder.UseSqlServer(connectionString);});
The options are now registered as scoped by default, so this code will run once each request to determine the connection string to use.
You could also put similar code into OnConfiguring if you prefer that pattern.
Reacted by nicolasr75, Thomas Koch, John Hamm, V. Jenks, Dimeji, Mohsen Shakiba, Matt Wagner, awaitJosh, Peter-Optiway, om-ha and 4 more
I use a similar aproach with ef core 1.x but tenant are separated by db schema.Same db different schemas. Shared tables like users groups etc... are in dbo schema.
Login use a context with only shared tables. After login the context is created with correct schema taken from user profile.
I wrote something about it here: #9965
If you need different connections you can use a separate database for login tables (and user connection configuration ) and a database for each customer (like others suggested to you). You will have to find a solution for shared tables (like a replication from login database).
Then i can set use databaseContext.IsMaintenance = true; to set the value in a given AsyncContext, and in the IsMaintenance flag by using the serviceProvider in the AddDbContextPool callback function.
Activity
ajcvickers commentedon Aug 30, 2017
@nicolasr75 Assuming you are using EF Core 2.0, then you can put logic into the code that builds the options. For example:
The options are now registered as scoped by default, so this code will run once each request to determine the connection string to use.
You could also put similar code into OnConfiguring if you prefer that pattern.
ste4net commentedon Oct 14, 2017
I use a similar aproach with ef core 1.x but tenant are separated by db schema.Same db different schemas. Shared tables like users groups etc... are in dbo schema.
Login use a context with only shared tables. After login the context is created with correct schema taken from user profile.
I wrote something about it here: #9965
If you need different connections you can use a separate database for login tables (and user connection configuration ) and a database for each customer (like others suggested to you). You will have to find a solution for shared tables (like a replication from login database).
eduardomar commentedon Jul 25, 2018
@ajcvickers There way to get a route attribute([Route("api/{CompanyCode}/[controller]")]) in this code?
ajcvickers commentedon Jul 25, 2018
@eduardomar That's an ASP.NET question, rather than an EF one. I don't know the answer.
Peter-Optiway commentedon Jul 12, 2019
I managed to refactor out some smelly code using the solution provided here, i combined it with
AsyncLocal.Then i can set use
databaseContext.IsMaintenance = true;to set the value in a given AsyncContext, and in the IsMaintenance flag by using theserviceProviderin the AddDbContextPool callback function.