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

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.

Похожее
Jul 26, 2023
Author: Jason Watmore
Built with .NET 7.0, Dapper 2.0 and PostgreSQLIn this tutorial we'll show how to build a .NET 7.0 (ASP.NET Core) API with Dapper and PostgreSQL that supports CRUD operations.Tutorial contents Example API overview Tools...
Dec 23, 2023
Author: Matt Bentley
A guide to implementing value objects — Domain-Driven Design’s most powerful, yet least understood and utilized building blockGenerated using DALL-E 3Value Objects are one of the most powerful building blocks in Domain-Driven Design for creating rich models, however programmers often...
Oct 24, 2022
Author: Anton Shyrokykh
Entity Framework Core is recommended and the most popular tool for interacting with relational databases on ASP NET Core. It is powerful enough to cover most possible scenarios, but like any other tool, it has its limitations. Long time people...
May 14, 2023
Author: Ravi Raghav
What is Kafka?Kafka is a distributed streaming platform developed by the Apache Software Foundation. It is designed to handle high-volume, real-time data streams and is commonly used for building data pipelines, stream processing applications, and real-time analytics.At its core, Kafka...
Написать сообщение
Почта
Имя
*Сообщение


© 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