In this blog, I write about Web Development, Blockchain, and DevOps. I am a Docker Captain, Public Speaker, and Developer Advocate at daily.dev. Check my YouTube channel.
Let's create a CRUD Rest API in C# (or C sharp), using:
.NET 7
ASP.NET (Framework for building web apps)
Entity Framework (ORM)
Postgres (Database)
Docker (Containerization)
Docker Compose (To run the database and the application)
Video version:
All the code is available in the GitHub repository (link in the video description): https://youtube.com/live/PY6uD1fgzZA
π Intro
Here is a schema of the architecture of the application we are going to create:
We will create 5 endpoints for basic CRUD operations:
Create
Read all
Read one
Update
Delete
Here are the steps we are going through:
Define the requirements
Create a new C# project
Install the dependencies
Configure the database connection
Create the logic for the CRUD operations
Update the project
Dockerfile
docker-compose.yml
Run the Postgres database and the application
Update the database schema
Test the application
We will go with a step-by-step guide, so you can follow along.
π Requirements:
.NET 7 installed and running
dotnet CLI
(Optional): C# Extension Pack for VS Code
π Create a new C# project
There are many ways to create a new C# project, but I will use the dotnet CLI
Open a terminal and run the following command:
dotnet new webapi -n csharp-crud-api
Now step into the directory:
cd csharp-crud-api
π¦ Install the dependencies
We will use the following dependencies:
Entity Framework
Npgsql (Postgres driver)
To install them, be sure to be in the csharp-crud-api directory and run the following commands:
Or you can replace the whole Program.cs file with the following code:
using Data;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Added configuration for PostgreSQLvar configuration = builder.Configuration;
builder.Services.AddDbContext<UserContext>(options =>
options.UseNpgsql(configuration.GetConnectionString("DefaultConnection")));
var app = builder.Build();
// Configure the HTTP request pipeline.if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
We are done with the application logic, now it's time to Dockerize it.
π³ Dockerize the application
We will Dockerize the application in creating and populating two files:
Dockerfile
docker-compose.yml
π Dockerfile``
At the root of the project create a new file called Dockerfile and populate it with the following code:
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /app
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS runtime
WORKDIR /app
COPY --from=build /app/out ./
ENTRYPOINT ["dotnet", "csharp-crud-api.dll"]
Explanation:
....
π docker-compose.yml
At the root of the project create a new file called docker-compose.yml and populate it with the following code: