• Jeff Fillegar
  • Posts
  • How to Build a .NET 6.0 API with PostgreSQL and EF Core

How to Build a .NET 6.0 API with PostgreSQL and EF Core

Introduction

.Net 6, the open source product of the Microsoft world, has entered our world with many new features. It offers a more useful and easier programming experience for developers than previous versions. In this article, we will quickly enter the world of .Net 6 with you. So without wasting any more time, let’s get started.

What will we Build?

In this project, we will develop an API in the .Net 6 project using Entity Framework – EF Core Code-First Approach. In the API project we will develop, we will adopt a layered architecture approach to reduce dependency. The application we will develop will perform CRUD (Create, Read, Update, Delete) operations to a database via API.

Tools Required

.Net 6 SDK

Visual Studio 2022

Docker (if not, local PostgreSQL can be use)

PostgreSQL Download

Project Setup

Now let’s move on to the installation phase of the project. We will base our project on the ‘User’ domain. With the project we will develop, we will be able to register new users, update, delete and read our database via API.

Since we have adopted the onion architecture approach, we will create 4 projects under a single solution. The projects are:

  • API: It is the layer that communicates with the client. Handles requests from the client

  • Service: It is the business layer. It is the area where we store the logics and algorithms that we want to develop within the scope of the project.

  • Repository: It is the layer that communicates with the database.

  • Core: It is the layer where we keep our models

Create New Visual Studio Projects

Then we will choose the name of the project we will create and the name of the solution.

dotnet6-name-peroject

Now we will choose the .Net version that we will use in the project. We proceed by choosing the .Net 6 version.

dotnet6-select-version

We have created the API layer, now we will create the Service, Repository and Core layers, respectively.

Right click on the user solution and go to ‘Add -> New Project’.

dotnet6-new-project

Then, let’s choose a Class Library type project and move on.

dotnet6-create-classlibrary

Now we will define the project name. Since we have created the Service layer, we will name the project as ‘User.Service’.

dotnet6-add-classlibrary

Finally, let’s choose the .Net version of the project we will create and create the project.

dotnet6-select-version-service

Let’s create the Repository and Core layers with the same approach. Then, let’s delete the default classes in the projects we created.

Code

We created layers that we will use in the project. Now it’s time to download the required libraries and write the code.

First of all, let’s remove Postgresql, which is the database we will use in the application, in docker. Let’s create a file named ‘docker-compose.yml’ in the file location of the solution and paste the following codes.

Let’s put the Postgresql database in a container in docker with the following command.

docker compose up -d

  • Microsoft.EntityFrameworkCore.Tools

  • Npgsql.EntityFrameworkCore.PostgreSQL

After installing the necessary libraries for the Repository layer, let’s move on to the Core layer and create our entity. We will right-click on the ‘User.Core’ project and create a new class by clicking ‘Add -> Class’.

Let’s give the class name as ‘UserInfo’ and paste the following code.

Now, let’s create the UserInfo table in our database with the Code First approach. We pass to the Repository layer again. Let’s create a class named ‘UserInfoDbContext’ here. This file is the class that will establish the database relationship with Entity Framework. Let’s write the following code in this class.

Now we’re going to create the class for our queries with the database. Let’s create a class called ‘UserInfoRepository’ and paste the following codes into it.

Before creating our ‘UserInfo’ model as a table in the database, we will move on to the API layer. Here we will define our database connection string to the ‘application.json’ file.

Time to match DbContext and our connection string in ‘Program.cs’ class

And our final move will be to add the following package to the API layer from NuGet.

  • Microsoft.EntityFrameworkCore.Design

dotnet6-efcore-api

Everything is ready to create the ‘UserInfo’ table in the database. We will open the Package manager console and select the ‘User.Repository’ project.

dotnet6-pm-console

Then we will run the ‘Add-Migration InitialDatab’ command.

dotnet6-efcore-migrate

After a successful migration, we will now update the database. Let’s run the ‘Update-Database’ command.

Now we have a table named ‘UserInfo’ in the Database. The next step is to create the Service layer. The Service layer we created will carry the data it receives from the API to the Repository layer.

In the ‘User.Service’ project we will create a class named ‘UserInfoService’. Then we will be writing the following codes into it.

After constructing the Service layer, our last step is to construct the API layer. Let’s right click ‘Controllers’ in the ‘User.API’ project and do ‘Add -> Controller’. Then let’s choose an Empty API Controller and name it ‘UserController’.

dotnet6-create-api

Now we will write the following codes in the ‘UserController’ class.

Finally, let’s add these two lines to the ‘Program.cs’ class.

Test

Let’s run the application and then go to the ‘/swagger/index.html’ path.

dotnet6-swagger

Conclusion

We have come to the end of our API development article with .Net 6. We have developed an application where we can perform user CRUD operations on a system very quickly. After you run the application, you can perform your tests with swagger. Here’s the source code.