Поиск  
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
Источник:
Просмотров:
4771
Configure multiple DBs with Repository pattern in .NET Core favorites 0

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.

Похожее
Sep 5, 2023
Author: Edson Moisinho
Simplifying data transport in C#. In modern C# development, data transport objects (DTOs) play a crucial role in exchanging information between different layers of an application, such as between a client and a server, and traditionally, developers have used classes...
Aug 8, 2024
Author: Davit Asryan
The growth of the internet has made instant communication technology more important than ever, especially for the Internet of Things (IoT). With so many devices like smart home gadgets and industrial sensors needing to talk to each other smoothly, having...
Feb 2, 2024
With the release of C# 10 in November 2021 developers were introduced to a new concept called records, in this post I’ll outline some of the key differences between records and classes. For this comparison I’ll only consider the default/common...
Jul 30, 2024
Author: Emer Kurbegovic
Azure Cosmos DB is a globally distributed, multi-model database service provided by Microsoft Azure. It supports multiple data models, including document, key-value, graph, and column-family data models. In this article, we’ll explore how to add and retrieve data from Azure...
Написать сообщение
Тип
Почта
Имя
*Сообщение
RSS
Если вам понравился этот сайт и вы хотите меня поддержать, вы можете
Soft skills: 18 самых важных навыков, которыми должен владеть каждый работник
Проблема понимания существующего кода, или Как делать иногда [не] надо
Как мы столкнулись с версионированием и осознали, что вариант «просто проставить цифры» не работает
Почему сеньоры ненавидят собеседования с кодингом, и что компании должны использовать вместо них
Функции и хранимые процедуры в PostgreSQL: зачем нужны и как применять в реальных примерах
Гороскоп для разработчиков
9 тяжёлых уроков, которые я усвоил за 18 лет разработки
Зачем нужен MediatR?
Выгорание эволюционирует. Что такое «тихий уход» — новый тренд среди офисных сотрудников
Как лучше проводить one-to-one со своими сотрудниками: 5 лайфхаков из личного опыта
Boosty
Donate to support the project
GitHub account
GitHub profile