Introduction
If you are learning ASP.NET Core, one of the first concepts you should understand is CRUD operations. CRUD is the foundation of almost every web application because it allows users to create, read, update, and delete data.
In this article , you will learn how to build a CRUD Web API in ASP.NET Core using Entity Framework Core (EF Core) with PostgreSQL following the Code First approach. By the end of this article, you'll be able to create your own REST API and connect it to a PostgreSQL database.
What is CRUD?
CRUD stands for:
Create – Add new data.
Read – Retrieve existing data.
Update – Modify existing data.
Delete – Remove data.
These four operations are the building blocks of most database-driven applications.
What is ASP.NET Core Web API?
ASP.NET Core Web API is a framework for building RESTful APIs. A Web API allows different applications, such as web apps, mobile apps, and desktop applications, to communicate with each other using HTTP requests. The commonly used HTTP methods are:
HTTP Method CRUD Operation Description
GET : Retrieves data from the database
POST: Adds new data
PUT : Updates existing data
DELETE: Removes data
Why Use Entity Framework Core?
Entity Framework Core (EF Core) is Microsoft's Object-Relational Mapper (ORM).Instead of writing SQL queries manually, EF Core lets you work with C# classes and objects. It automatically converts your C# code into SQL queries.
Why PostgreSQL?
PostgreSQL is a powerful, open-source Relational Database Management System (RDBMS) used to store, organize, and manage data efficiently.
It supports SQL, provides high security and reliability, and is widely used for web, enterprise, and business applications.
Prerequisites
Before starting, install the following NuGet packages:
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Tools
Npgsql.EntityFrameworkCore.PostgreSQLAfter installing the packages, create a new ASP.NET Core Web API project.
Step 1: Configure the PostgreSQL Connection
Open pgAdmin 4 and make sure the PostgreSQL server is running.

Open appsettings.json and add the following connection string.
{ "ConnectionStrings": {
"myConn": "Host=localhost;Port=5432;Database=cruddb;Username=postgres;Password=admin"
}}Step 2: Create the Model Class
Create a Product model inside the Models folder.
using System.ComponentModel.DataAnnotations;
namespace CrudApp.Model
{
public class Product{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
}}Step 3: Create the DbContext Class
Create an AppDbContext class that inherits from DbContext.
The DbContext acts as a bridge between your application and the PostgreSQL database.
Inside the class:
Inherit from DbContext
Create a constructor
Add a DbSet<Product> property


Join the conversation! Your thoughts help the community grow.