A .NET demonstration project showing how to manage multiple Entity Framework Core database providers in a single application using separate migration projects for Microsoft SQL Server and PostgreSQL.
This approach allows the application to use different database providers while keeping their database migrations isolated and independently managed.
- Overview
- Project Structure
- Prerequisites
- Creating Database Migrations
- Running the Application
- API Endpoint
- References
- Author
The project demonstrates how to configure Entity Framework Core to support multiple database providers, with a dedicated migration project for each provider.
Supported database providers:
- Microsoft SQL Server
- PostgreSQL
| Database Provider | Migration Project |
|---|---|
| Microsoft SQL Server | MultipleDbProvider.Infrastructure.SqlServerMigrations |
| PostgreSQL | MultipleDbProvider.Infrastructure.PostGreMigrations |
Each migration project maintains its own database migration history, allowing schema changes to be generated separately for each provider.
The solution contains the main application project and separate migration projects for each supported database provider.
MultipleDbProviderDemo/
├── MultipleDbProvider/
├── MultipleDbProvider.Infrastructure.SqlServerMigrations/
└── MultipleDbProvider.Infrastructure.PostGreMigrations/
Note: This tree illustrates the project layout based on the project names used in the migration commands.
Before getting started, ensure you have:
- The required .NET SDK installed.
- Visual Studio or another compatible .NET development environment.
- Access to a Microsoft SQL Server or PostgreSQL instance.
- The Entity Framework Core CLI tool (
dotnet-ef) installed globally.
Install the EF Core CLI tool using:
dotnet tool install --global dotnet-efIf it is already installed, you can update it with:
dotnet tool update --global dotnet-efOpen a terminal in the solution's root directory:
MultipleDbProviderDemo
Choose the appropriate command based on the database provider for which you want to generate migrations.
Run the following command to create a migration for SQL Server:
dotnet ef migrations add "Initial Db" \
-s "MultipleDbProvider" \
-p "MultipleDbProvider.Infrastructure.SqlServerMigrations" \
-- --provider SqlServerThis generates the migration in the MultipleDbProvider.Infrastructure.SqlServerMigrations project.
Run the following command to create a migration for PostgreSQL:
dotnet ef migrations add "Initial Db" \
-s "MultipleDbProvider" \
-p "MultipleDbProvider.Infrastructure.PostGreMigrations" \
-- --provider PostGreSqlThis generates the migration in the MultipleDbProvider.Infrastructure.PostGreMigrations project.
For future schema changes, replace Initial Db with a descriptive migration name, for example:
AddProductDescription
Generate migrations separately for each database provider to keep their schemas synchronized with the application's model changes.
Note: Creating a migration generates migration files; it does not automatically apply the changes to the database.
Follow these steps to run the project:
- Open the solution in Visual Studio.
- Update
appsettings.jsonto select the desired database provider. - Configure the connection string for the selected database.
- Build the solution.
- Run the
MultipleDbProviderproject.
Ensure that the configured database is accessible and that the appropriate migrations have been applied before testing database-dependent endpoints.
Once the application is running, access the Products API using the following endpoint:
GET Products
https://localhost:7068/api/Products
The URL assumes the application is configured to use HTTPS on port 7068.
Sanjit Raj Shakya
Website: https://sanjitrajshakya.com.np
If you find this project useful, feel free to explore, learn, and build upon it.
Thank you!