Concepts covered
-
ASP.NET Core MVC: A powerful and open-source web framework for building modern, dynamic, and cross-platform web applications using the ASP.NET framework.
-
Identity (ASP.NET Core Identity): A membership system that adds login functionality to ASP.NET Core applications, providing user authentication, authorization, and account management features.
-
PostgreSQL: An advanced, open-source relational database management system known for its extensibility, standards compliance, and robust support for complex queries, making it a popular choice for web applications.
-
Migration - Migrating your ASP.NET Core MVC application from SQL Server to PostgreSQL can bring flexibility and scalability to your database. In this guide, I will walk through the process, covering key steps to ensure a smooth transition.
Follow the below steps
Delete the existing Migration folder to start with a clean slate. This ensures that the new PostgreSQL migrations will be generated from scratch.
# Delete the Migration folder
rm -rf Migrations
To enable PostgreSQL support, add the required NuGet packages to your project file:
<!-- Microsoft.VisualStudio.Web.CodeGeneration.Design -->
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.11" />
<!-- Npgsql.EntityFrameworkCore.PostgreSQL -->
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.0" />
Use the dotnet restore
command to fetch the newly added packages.
dotnet restore
Adjust the connection string in your appsettings.json
or appsettings.Development.json
to reflect the PostgreSQL server details:
"DefaultConnection": "Host=localhost;Port=5433;Database=authentication_app;Username=postgres;Password=<YOURPASSWORD>;",
Ensure that the host, port, database name, username, and password match your PostgreSQL setup.
Replace the SQL Server configuration in your Startup.cs
file with PostgreSQL configuration. Locate the code that configures the DbContext, and replace it as follows:
// Replace the existing SQL Server configuration
builder.Services.AddDbContext<AuthenticationAppDbContext>(options => options.UseSqlServer(connectionString));
// With the new PostgreSQL configuration
builder.Services.AddDbContext<IdentityContext>(options => options.UseNpgsql(connectionString));
// Add Migration
Add-Migration "Initial Create"
// Update-Database - This will create physical databases
Update-Database
This change ensures that your application now uses PostgreSQL as the underlying database.
Congratulations! You've successfully migrated your ASP.NET Core MVC application from SQL Server to PostgreSQL. Ensure to thoroughly test your application to verify that the migration was successful, and your functionalities remain intact.
By following these steps, you've embraced the power of PostgreSQL, providing your application with a robust and scalable database solution.
Happy coding!