This project has been configured with Entity Framework Core migrations for safe, controlled database schema management. The setup includes a baseline migration approach for existing databases, ensuring compatibility with both local development and production deployment.
- ✅ Added
Microsoft.EntityFrameworkCore.Designpackage - ✅ Created baseline migration for existing database schema
- ✅ Configured local development environment
- ✅ Integrated with existing production deployment pipeline
- ✅ Existing Database Integration - No database recreation required
- ✅ Baseline Migration -
20250826224824_InitialSchemaWithProperDeleteBehaviorcaptures current schema with safe delete behavior - ✅ Local Development Ready - Migrations work with existing local database
- ✅ Production Compatible - Pipeline already configured for EF migrations
- ✅ Zero automatic deletions - EF only adds, never removes
- ✅ Existing data preserved - All current records remain intact
- ✅ Migration history tracking -
__EFMigrationsHistorytable properly configured - ✅ Pipeline control - Migrations run automatically during deployment
- ✅ Safe delete behavior - Explicit configuration prevents unwanted cascade deletes
- ✅ RESTRICT by default - Prevents accidental data loss from parent deletions
- ✅ Logical cascades only - Pathfinder deletion removes their achievements/honors
- ✅ Protected reference data - Cannot delete clubs, honors, achievements if in use
- ✅ Explicit configuration - All relationships explicitly defined in
OnModelCreating
# 1. Ensure baseline migration is applied (one-time setup)
psql -h localhost -U dbuser -d pathfinder -c "INSERT INTO \"__EFMigrationsHistory\" (\"MigrationId\", \"ProductVersion\") VALUES ('20250826224824_InitialSchemaWithProperDeleteBehavior', '9.0.8');"
# 2. Verify migration status
dotnet ef migrations list --project PathfinderHonorManager
# 3. Make model changes and create new migrations
dotnet ef migrations add AddNewFeature --project PathfinderHonorManager
# 4. Test locally
dotnet ef database update --project PathfinderHonorManagerThe existing GitHub Actions pipeline (.github/workflows/main_pathfinderhonormanager.yml) already includes:
- EF migrations integration -
dotnet ef database update --connection "${{ secrets.PRODCONNECTIONSTRING }}" - Automatic migration application - Runs before app deployment
- Zero manual intervention - Migrations apply automatically
- Migration:
20250826224824_InitialSchemaWithProperDeleteBehavior - Purpose: Represents the current database schema state with safe delete behavior
- Status: Manually marked as applied in
__EFMigrationsHistory - Approach: Standard EF Core baseline migration pattern with explicit delete behavior configuration
- Existing Database - Your database already has the schema
- No Recreation - Avoids "relation already exists" errors
- Future Migrations - Enables normal EF Core migration workflow
- Production Ready - Pipeline will recognize this as baseline
# 1. Make model changes in your C# code
# 2. Create new migration
dotnet ef migrations add DescriptiveMigrationName --project PathfinderHonorManager
# 3. Review generated migration file
# 4. Test locally (optional)
dotnet ef database update --project PathfinderHonorManager
# 5. Commit and push
git add .
git commit -m "Add new feature with migration"
git push origin main# Check migration status
dotnet ef migrations list --project PathfinderHonorManager
# Apply pending migrations
dotnet ef database update --project PathfinderHonorManager
# Generate SQL script for review
dotnet ef migrations script --project PathfinderHonorManager --output migration.sqlPathfinderCS- Database connection stringAuth0:Domain- Auth0 domainAuth0:Audience- Auth0 audienceAuth0:ClientId- Auth0 client ID
- Local:
Host=localhost;Database=pathfinder;Username=dbuser;Password=yourpassword - Production: Configured via GitHub Secrets (
PRODCONNECTIONSTRING)
/health- Overall health status/health/ready- Readiness check/health/live- Liveness check
# Check current migration status
dotnet ef migrations list --project PathfinderHonorManager
# Check database state
dotnet ef database update --dry-run --project PathfinderHonorManagerCause: Migration trying to create tables that already exist
Solution: Ensure baseline migration is marked as applied in __EFMigrationsHistory
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES ('20250826224824_InitialSchemaWithProperDeleteBehavior', '9.0.8');Cause: Local database out of sync with migration files
Solution: Check __EFMigrationsHistory table and sync accordingly
- ✅ Baseline Migration - One-time setup for existing databases
- ✅ Descriptive Names - Use clear migration names like
AddUserProfileTable - ✅ Small Changes - Keep migrations focused and incremental
- ✅ Local Testing - Test migrations locally before committing
- ✅ Pipeline Integration - Migrations run automatically
- ✅ No Manual Steps - Production deployment is fully automated
- ✅ Health Monitoring - Use health checks to verify deployment
- ✅ Backup Strategy - Ensure database backups before major changes
- ✅ Migration History - Monitor
__EFMigrationsHistorytable - ✅ Clean Up - Remove old migration files when no longer needed
- ✅ Documentation - Document complex schema changes
- ✅ Version Control - Keep migration files in source control
# 1. Stop application
# 2. Rollback database to previous migration
dotnet ef database update PreviousMigrationName --project PathfinderHonorManager
# 3. Redeploy previous application version
# 4. Verify data integrity# 1. Create rollback migration
dotnet ef migrations add RollbackFeature --project PathfinderHonorManager
# 2. Test rollback locally
# 3. Deploy through pipeline
# 4. Monitor health checks- Creates database from scratch
- First migration creates all tables
- Works with empty databases
- Works with existing database
- Baseline migration represents current state
- Future migrations are incremental
- No database recreation required
For issues or questions:
- Check migration status:
dotnet ef migrations list - Verify
__EFMigrationsHistorytable contents - Test locally with development database
- Check pipeline execution logs
- ✅ Baseline Migration Complete - Local development ready
- ✅ Production Pipeline Ready - Migrations will run automatically
- Create New Features - Add new migrations as needed
- Monitor Deployments - Verify migrations apply successfully
- Team Documentation - Share this workflow with your team
Added EF Core specific entries:
# Entity Framework Core
*.migration.sql
migration.sql
baseline-migration.sql
*.efmigrations
- ✅
Migrations/folder - All migration files - ✅
*.csmigration files - Migration classes - ✅
*.Designer.csfiles - Migration designer files - ✅
PathfinderContextModelSnapshot.cs- Model snapshot
- ❌
*.migration.sql- Generated SQL scripts - ❌
baseline-migration.sql- Temporary baseline script - ❌
*.efmigrations- EF Core artifacts