Overview
The XGP Photo API uses PostgreSQL as its database with Entity Framework Core for data access and migrations. This guide covers:- PostgreSQL installation and configuration
- Connection string setup
- Running Entity Framework migrations
- Database seeding
Prerequisites
- PostgreSQL 16 or later
- .NET 9.0 SDK
- Entity Framework Core CLI tools
If you’re using Docker, skip to the Docker Setup section for a faster setup.
PostgreSQL Installation
- Windows
- macOS
- Linux (Ubuntu/Debian)
- Docker
- Download PostgreSQL from postgresql.org
- Run the installer and follow the setup wizard
- Remember your postgres user password
- Default port is 5432
Database Configuration
Create Database
Connect to PostgreSQL and create the database:Connection String
Configure the connection string inappsettings.json:
appsettings.json
DbContext Configuration
TheApplicationDbContext extends IdentityDbContext for authentication support:
Data/ApplicationDbContext.cs
Entity Framework Migrations
Install EF Core Tools
Migration History
The API includes these migrations:InitIdentity
Creates ASP.NET Identity tables for authentication:
- AspNetUsers
- AspNetRoles
- AspNetUserRoles
- AspNetUserClaims
- AspNetRoleClaims
- AspNetUserLogins
- AspNetUserTokens
InitIdentity Migration
This migration sets up the authentication infrastructure:Migrations/20251016083348_InitIdentity.cs
AddProjectsModule Migration
This migration creates the project tables:Migrations/20251016120232_AddProjectsModule.cs
Running Migrations
Apply All Migrations
Verify Migration Status
Create New Migration
If you modify entities:Rollback Migration
Database Seeding
Seed Initial Data
Create admin user and roles on application startup:Program.cs:
Docker Setup
Use the provideddocker-compose.yml for easy setup:
docker-compose.yml
Start with Docker Compose
The API automatically applies migrations on startup when running in Development mode.
Connection String Formats
Local Development
Docker Container
When API runs in Docker, use the service name:Production (with SSL)
Connection Pooling (Production)
Database Schema
Tables Overview
Identity Tables (ASP.NET Core Identity)
Identity Tables (ASP.NET Core Identity)
- AspNetUsers - User accounts
- AspNetRoles - Role definitions (Admin, User, etc.)
- AspNetUserRoles - User-to-role assignments
- AspNetUserClaims - Additional user claims
- AspNetRoleClaims - Role-based claims
- AspNetUserLogins - External login providers
- AspNetUserTokens - Authentication tokens
Project Tables
Project Tables
- Projects - Photography projects with metadata
- ProjectDetails - Detail images for each project (1:N relationship)
Entity Relationship Diagram
Troubleshooting
Connection refused
Connection refused
Error:
Npgsql.NpgsqlException: Connection refusedSolutions:- Verify PostgreSQL is running:
sudo systemctl status postgresql - Check port is correct (default: 5432)
- Verify firewall allows connections
- Check pg_hba.conf for authentication settings
Password authentication failed
Password authentication failed
Error:
password authentication failed for user "postgres"Solutions:- Verify password in connection string
- Reset PostgreSQL password if needed
- Check POSTGRES_PASSWORD environment variable in Docker
Database does not exist
Database does not exist
Error:
database "XgpPhotoDb" does not existSolutions:- Create database manually:
CREATE DATABASE "XgpPhotoDb"; - Or let the API create it automatically on first run
Migration already applied
Migration already applied
Error:
Migration '...' has already been appliedSolutions:- Check migration history:
dotnet ef migrations list - Remove migration if not needed:
dotnet ef migrations remove - Or specify target migration:
dotnet ef database update TargetMigration
Best Practices
Next Steps
Deployment
Deploy the API with Docker
Authentication
Set up JWT authentication