I will try to explain how to install PostgreSql on windows pc and how to change available .net web api project database with PostgreSql. Firstly, let’s download and install PostgreSql. You can download it from here. Installation screens is as follows.
When you click finish button, second installation will start. Second installation is for administration tools. I skipped this step because I installed them before. But you should continue, because I used pgAdmin afterwards. It’s mean, you will see screenshots from pgAdmin. Anyway, let’s check the PostgreSQL. Click to start menu on windows and search pgAdmin from my computer. It’s browser based management panel for pgAdmin. pgAdmin screenshot is as follows.
Now, I will create a new database for my web api via pgAdmin. The screenshot is as follows.
After create a database, firstly I runned as follows command.
dotnet tool install –global dotnet-ef
I have already Entity Framework library in my project. Now, I will add a sub library. That version should be same with available Entity Framework. It’s as follows.
dotnet add package Microsoft.EntityFrameworkCore.Design
I need one more library for this example. I will include PostgreSQL to my project. This library is as follows.
dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL –version 3.1.1
Before database migration and update, I should add new line on my DBContext file. New line will be on onConfiguring method.
Now, Everything is ready for migration and update. You should run this command on your Data Access project.
dotnet ef migrations add example
The screenshot is as follows.
Let’s run last command and then database will be ready.
Let’s return pgAdmin and check tables. The screenshot is a follows.
Now, database and connection ready for use. Let’s run web api and check connection.