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

Shivam Tiwari 11 Reputation points
2020-11-26T07:58:03.497+00:00

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

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,164 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,245 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Nuno Pereira 1 Reputation point
    2020-11-26T12:39:02.893+00:00

    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. Bruce (SqlWork.com) 55,686 Reputation points
    2021-09-20T17:08:26.45+00:00

    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.

    0 comments No comments