question

ShivamTiwari-4600 avatar image
0 Votes"
ShivamTiwari-4600 asked Bruce-SqlWork edited

How to Get multiple database DBContext service in .net core MVC 3.1 dynamically as per request and controller calls

Hello Coders,

I want to know how to connect with multiple databases in web application in .net core MVC 3.1 dynamically as per request and create DBContext. so that I can access the specific database on my controller call as a service. (In startup File)

also please let me know the impact if I will create multiple DB context services with different databases over on startup.cs on configureservices() method file.

You help will be appreciated....

Thanks in advance and Happy coding

dotnet-csharpdotnet-aspnet-core-general
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Whether you are using the same kind of database or not? If using different kind of database, you have to use different Database Providers and register multiple DBcontext on the Startup. If you are using the same kind of database, you could register multiple dbcontext on the Startup, or you could try to use a DbContext factory. By using multiple DBcontexts, it can make your design much cleaner and separate different functional areas, but it might have some issues such as share entity type between dbcontext or transfer the entities from one context to another.

0 Votes 0 ·
NunoPereira-0641 avatar image
0 Votes"
NunoPereira-0641 answered DivyashreeG-2357 commented

Your question isn't very clear on what you are trying to achieve.
If your scenario is a multitenant scenario with database per tenant then you could create an ITenantResolver that would use the authentication token to resolve the tenant (as an example, you could use whatever logic you require to resolve the tenant) and inject the ITenantResolver into to an IConnectionStringResolver that would select the correct ConnectionString for the resolved tenant. The IConnectionStringResolver could then be injected into the DbContext that would use it to configure the connection. That way you could inject DbContext into the controller or the controller action as you normally do. The ITenantResolver can also be used whenever you need to know the current tenant.
If this isn't your scenario then I would say to have a look at the DbContextFactory and check if that fits your needs (the ITenantResolver and IConnectionStringResolver can be used along with DbContextFactory).
If you give some more details on your scenario and what you are trying to achieve I can help you with more details.

Hope this helps

Edit: I'm assuming that your question is related to having the same DbContext type to connect to different databases. If your question is about having multiple DbContext types that each connect to different databases, then you just need to register the multiple DbContext types on Startup, each one with the corresponding ConnectionString and inject the one(s) you need in the controller or the controller action.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I have an application that required to communicate with multiple databases but the database whom I want to connect will be decided at runtime dynamically as per the request parameter. Also I need to use this scenario as creating DBContext service at startup.cs.

in the second paragraph I asked another question and this scenario I am currently using but don't know its performance issue in the application.


0 Votes 0 ·

Hello, I am trying to achieving something similar to this. Let me get the scenario first.

We connect the application to particular database based on the user input. i.e., we give form for user to select a database with which they need to interact or perform CRUD operations with and based on their selection we connect them to DB. IS this what the scenario is? If so did you try to resolve it? Please let me know

0 Votes 0 ·
Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered Bruce-SqlWork edited

typically you would inject a DbContext factory rather than an actual context. the action / controller would call the factory to get a context.

you don't specify how the request would map to a particular database, but typically you would generate a key to pass to the factory to return a DbContext.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.