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
Источник:
Просмотров:
1545

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.

Похожее
Oct 27, 2022
Author: Sebastian Streng
Writing code can be very exciting but it also can be very frustrating if your code is based on nested loops. Iterations are still one the most important parts of coding. So how can we avoid using ugly nested loops...
Mar 18
Author: codezone
File reading operations in C# are crucial for many applications, often requiring efficiency and optimal performance. When handling file reading tasks, employing the right strategies can significantly impact the speed and resource utilization of your application. Here are some best...
May 31, 2023
Author: Anton Selin
LINQ (Language Integrated Query) is a powerful querying tool in .NET that allows you to perform complex queries directly in C#.The System.Linq.Expressions namespace is a part of LINQ that provides classes, interfaces, enumerations and structures to work with lambda expressions...
Feb 1
Author: Sohail Aslam
IntroductionWelcome to the world of C#! Whether you’re a seasoned developer or just starting your programming journey, the power and versatility of C# can elevate your coding experience. In this article, we’ll explore a curated collection of tips and tricks...
Написать сообщение
Почта
Имя
*Сообщение


© 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