46  
netcore
Advertisement
Поиск  
Always will be ready notify the world about expectations as easy as possible: job change page
Nov 16, 2022

Configure Multiple DBs with Repository pattern in .Net Core

Автор:
Vipin Sharma
Источник:
Просмотров:
1619

Need to communicate with multiple DBs in one application??

Usually we don’t face such scenarios. However if such requirement comes in picture, we will see how easy it is to communicate with multiple DBs using repository pattern in .Net Core Web API.

To configure mongoDB, we need to install mongoDB driver for C#, this we can install via NuGet package manager. After installation we need connection string and database name which we usually define in our appsettings.json file as shown below:


Connection string

We have connection string and DB name for two databases. Now let’s configure them in ConfigureServices method in startup.cs file


MultipleDbContext.cs


Startup.cs

Now we need our repository through which our business layer will communicate to perform CRUD operations in DB. Let’s create repository and add following configuration:


Repository.cs

We are almost done. So far we have configured multiple DBs and also configured our repository to communicate with those DBs. Only thing is left to perform CRUD operations via repository.


Get data from first DB


Get data from second DB

In our above two snippets, we can see how easy it is to get data from both First DB and Second DB. Only change we did is collection name which has been attached to their individual DB context. We can perform other CRUD operations in similar fashion.

Похожее
Nov 7, 2020
Author: Rebai Hamida
IntroductionDuring this article, we will learn how to work with the microservice architecture patterns and Docker containers using the .NET Core 3 platform to build a distributed system.Monolithic vs Microservices ArchitectureBefore talking about Microservices, we need to understand the difference...
Mar 28, 2023
Author: Anupam Maiti
IntroductionThis article demonstrates Middleware concepts in ASP.NET Core. At the end of this article, you will have clear understanding on below points: What is Middleware? Why Middleware ordering is important? Understanding...
Jun 5, 2023
Author: Juan Alberto España Garcia
In this section, we’ll explore the world of unit testing in C# and .NET, learn what unit testing is, why it’s important, and the landscape of testing frameworks and tools available to developers.What is Unit Testing?Unit testing is the process...
Sep 14, 2023
Author: Mina Pêcheux
Interfaces are at the heart of the “composition-over-inheritance” paradigm — let’s see what that means!As you probably know, C# is a statically typed language. And as such, it is very helpful with type-checking and safe data conversions. Your IDE most...
Написать сообщение
Почта
Имя
*Сообщение


© 1999–2024 WebDynamics
1980–... Sergey Drozdov
Area of interests: .NET Framework | .NET Core | C# | ASP.NET | Windows Forms | WPF | HTML5 | CSS3 | jQuery | AJAX | Angular | React | MS SQL Server | Transact-SQL | ADO.NET | Entity Framework | IIS | OOP | OOA | OOD | WCF | WPF | MSMQ | MVC | MVP | MVVM | Design Patterns | Enterprise Architecture | Scrum | Kanban
LinkedIn
Boosty
Donate to support the project
GitHub account
GitHub profile