This project demonstrates the implementation of a .NET web application that interacts with multiple databases using Entity Framework Core. The primary goal is to showcase how a .NET application can manage relationships across different databases and provide a clean, maintainable code structure that adheres to best practices and the SOLID principles.
In modern enterprise applications, it is not uncommon to have data spread across multiple databases. For instance, a company might store user data in one database, product information in another, and transactions in a third. Managing relationships and ensuring data integrity across these databases can be challenging, particularly when using an ORM like Entity Framework Core, which traditionally assumes a single database context.
This project provides a comprehensive solution to this problem by:
- Implementing a three-tier architecture (Presentation, Service, and Data layers) to ensure clean separation of concerns.
- Utilizing Entity Framework Core to interact with multiple databases.
- Providing a mechanism to handle relationships across different databases outside of EF Core's foreign key constraints.
The project is divided into three main layers:
-
Data (Data Layer):
- Contains the data models and DbContext configurations.
- Manages the database interactions using EF Core.
-
Services (Service Layer):
- Implements business logic and data manipulation.
- Includes a unit of work and repository pattern to abstract data access.
- Handles mapping of data transfer objects (DTOs).
-
BlazorWebUI (Presentation Layer):
- ASP.NET Core Blazor application for the user interface.
- Includes pages for managing users, products, transactions, and categories.
- Handles form validation and user interactions.
You can debug the app locally to see how it all work. You can view the web application here: Live Demo, and you can also explore the web API here: Web API. Please note that it's possible that the sites are deactivated when you visit it. In that case, please contact me so I can reactivate it.
However, if you'd like to run it locally or if you want to debug it, please continue reading.
- .NET 8 SDK
- Visual Studio 2022 (or later)
-
Clone the repository:
git clone https://github.com/azayrahmad/3TierAppWithMultiDatabases.git cd 3TierAppWithMultiDatabases
-
Configure the databases: Run these commands to generate embedded SQLite databases.
dotnet ef migrations add InitialCreate --project Data --context ProductDbContext --startup-project Services dotnet ef migrations add InitialCreate --project Data --context UserDbContext --startup-project Services dotnet ef migrations add InitialCreate --project Data --context TransactionDbContext --startup-project Services dotnet ef database update --project Data --context ProductDbContext --startup-project Services dotnet ef database update --project Data --context UserDbContext --startup-project Services dotnet ef database update --project Data --context TransactionDbContext --startup-project Services
-
Run the application:
- Open the solution in Visual Studio.
- Set
Services
andBlazorWebUI
as the startup projects and run both of them. - Alternatively run these commands each in two different consoles.
dotnet run --project Services -lp https dotnet run --project WebUI -lp https
If all goes well, you will be greeted with the homepage.
If you run it locally, you also might be able to see the Swagger page for the backend.
This project provides a robust solution for managing a .NET application with multiple databases using Entity Framework Core. By following the provided steps, you can deploy a scalable and maintainable web application that demonstrates best practices and effective use of modern technologies.
For any questions or contributions, please refer to the issues section of the repository.