Week 3–4: Database with PostgreSQL
Replace the in-memory list with a real PostgreSQL database running in Docker. Learn Entity Framework Core and database migrations.
Essential (75% of time)
Section titled “Essential (75% of time)”Core skills:
- Run PostgreSQL in Docker
- Create a DbContext with Entity Framework Core
- Create and apply migrations
- Replace in-memory List with database queries
- Set up a one-to-many relationship (Author → Books)
Start PostgreSQL locally
Section titled “Start PostgreSQL locally”docker run --name books-db \ -e POSTGRES_USER=bookuser \ -e POSTGRES_PASSWORD=dev123 \ -e POSTGRES_DB=bookdb \ -p 5432:5432 \ -v books-data:/var/lib/postgresql/data \ -d postgres:16The -v flag creates a persistent volume so your data survives container
restarts.
Key resources
Section titled “Key resources”- PostgreSQL Docker Image
- EF Core Getting Started
- Npgsql EF Core Provider
- EF Core Migrations
- Relationships in EF Core
- Docker Getting Started
LLM prompts
Section titled “LLM prompts”“Show me step-by-step: install EF Core with PostgreSQL, create a DbContext, configure connection string in appsettings.json.”
“Create Book and Author entities where Author has many Books. Include navigation properties for Entity Framework Core.”
“Explain the migration workflow: how to create a migration, view the SQL, and apply it to PostgreSQL. What commands do I run?”
“Replace this in-memory code with EF Core:
var book = books.FirstOrDefault(b => b.Id == id). Use async/await.”
Brief (15% of time)
Section titled “Brief (15% of time)”- View database tables in Azure Data Studio
- Seed initial test data
- Use
Include()to load related data
Optional (skip if needed)
Section titled “Optional (skip if needed)”- Understanding the generated SQL
- Migration rollback
- PostgreSQL-specific features
Practical project
Section titled “Practical project”Convert the Books API to use a real PostgreSQL database with an Authors table.
Learning activities
Section titled “Learning activities”- Start the PostgreSQL container using the command above
- Verify it is running:
docker ps - Install NuGet packages:
Npgsql.EntityFrameworkCore.PostgreSQLandMicrosoft.EntityFrameworkCore.Design - Create
AuthorandBookentity classes with a navigation property - Create a
BooksDbDbContext withDbSet<Book>andDbSet<Author> - Add connection string to appsettings.json
- Register DbContext in Program.cs
- Run
dotnet ef migrations add InitialCreate - Run
dotnet ef database update - Connect to the database in Azure Data Studio and view the created tables
- Replace all
List<Book>code in endpoints with async DbContext calls - Test all endpoints still work in Swagger
Useful Docker commands
Section titled “Useful Docker commands”docker ps # Check if container is runningdocker stop books-db # Stop the containerdocker start books-db # Start it again (data is preserved)docker logs books-db # View logs if something is wrongKey concepts to master
Section titled “Key concepts to master”- Starting, stopping, and checking Docker containers
- DbContext setup and registration in Program.cs
- Entity classes and navigation properties
- One-to-many relationships and foreign keys
- Migration commands:
migrations add,database update - Async database operations:
ToListAsync(),FindAsync(),SaveChangesAsync()
Deliverable
Section titled “Deliverable”Books API reads and writes from PostgreSQL. Authors table exists with a relationship to Books.
Week 1–2: Minimal APIs | Back to Overview | Week 5: Validation