This is a great Sunday of a very hot Summer. We have gone through the overview of the .net core framework in our previous article. And today I will walk you through how we can use the ASP.NET Core Boilerplate with PostgreSQL database.
To kickstart your project in 5 simple steps, let’s start with the overview of Asp.Net Boilerplate.
Overview of ASP.NET Boiler Plate
ASP.NET Boilerplate is a general purpose application framework specially designed for new modern web applications. It uses already familiar tools and implements best practices around them to provide you a SOLID development experience.
It includes following features inbuilt when you download your startup template from the aspnet boilerplate website
- Layered Architecture
- Modular design
- Domain Driven Design
- Comprehensive Documentation
To go through in detail documentation of Aspnet Boilerplate go through this link
Steps to download your startup template for boilerplate
- Go to https://aspnetboilerplate.com/Templates
- Choose between the two options available to go with Asp.net core or normal Asp.net MVC framework
- We have the different sub options available under that two categories to choose, We can choose to go with Angular JS or Vue JS template to build Single Page application. We can also go for Multi-Page Application with HTML, jQuery, and ASP.NET MVC.
- Next we need to choose the Project name. It will be used to create the namespace of the different layer of projects added as a part of the architecture.
- Add the captcha and click on Create My Project button. It will download your sample template to move ahead with your development.
- Here in this blog, I have used Asp.Net Core with Multi-Page Web Application 3.6.0 version.
The downloaded template contains total 8 projects as shown below.
The default template considers the MS SQL server as a default database server. Now as we wanted to use PostgreSQL we need to do some modifications in the template.
1. Add nugget package in Entity framework Project
Navigate to the Step2Dev.Demo.EntityFrameworkCore project and click on Manage NuGet Packages from dependencies.
Install Npgsql.EntityFrameworkCore.PostgreSQL package (Version 2.0.1) from nuget.org
2. Replace UseNpgsql with UseSqlServer
Go to DbContextConfigure.cs file in the Entity FrameworkCore project. You will see two overloaded Configure methods over there. You just need to replace the builder.UseSqlServer function with builder.UseNpgsql method.
3. Update Migrations
The Entity framework core Project also contains the initial Migrations. That is created to use with SQL Server Database. We need to change it to use it with PostgreSQL
- Search for “SqlServer:ValueGenerationStrategy” in the entire project and replace all the occurrences of that with “Npgsql:ValueGenerationStrategy”
- Now search for IdentityColumn in the entire project and replace the same with NpgsqlValueGenerationStrategy.SerialColumn. This will allow you to create the identity column in the table.
- Update the MaxLength for the “AbpLanguageTexts” table in all Migration files. In the Initial Migrations, they have defined the MaxLength of Value field in above table as 67108864. But PostgreSQL supports Maximum 10485760 length for varchar Type. So we need to override the max length of the value field.
Make sure you do this replacement in all the files available under Migration folder.
4. Change in Connection String
The template contains one project for the front-facing web application using MVC (Step2Dev.Demo.Web.MVC) and another one to expose the Web API (Step2Dev.Demo.Web.Host)
Open the appsettings.json file in both the projects. It will contain the default connection string as shown below.
"Default": "Server=localhost; Database=DemoDb; Trusted_Connection=True;"
We need to change it with the below connection string of PostgreSQL Server
"Default": "User ID=youruserid;Password=yourpassword;Host=localhost;Port=5432;Database= DemoDb;Pooling=true;"
Make sure you do this change in both the project’s appsettings.json file
5. Run Migration using Update Database
Now run the migrations using “update-database –verbose” command from the Package Manager Console. Make sure you select the Entity Framework Core project while running the Migrations.
6. Check the database in pgAdmin
There you go!!! You are all set to go with your template now, which will work with PostgreSQL.
Happy Coding from Step2Dev.