managing-migrations
Manages Entity Framework Core migrations using project scripts. Use when creating, applying, listing, or removing database migrations.
When & Why to Use This Skill
This Claude skill streamlines Entity Framework Core (EF Core) migration management by providing a standardized, script-based interface for database schema evolution. It automates complex 'dotnet ef' commands through pre-configured Bash and PowerShell scripts, ensuring consistent project paths, connection strings, and naming conventions across development environments. By abstracting the CLI complexity, it minimizes human error and enhances productivity for .NET developers managing relational database schemas.
Use Cases
- Generating new database migrations automatically after modifying C# entity configurations or data models.
- Synchronizing local development databases with the latest project schema by applying pending migrations.
- Auditing migration history and status to verify which schema changes have been successfully applied to the environment.
- Safely removing the most recent unapplied migration to correct design errors without manual CLI flag configuration.
- Resetting the development database to a clean state by dropping and recreating it with all migrations for testing purposes.
| name | managing-migrations |
|---|---|
| description | Manages Entity Framework Core migrations using project scripts. Use when creating, applying, listing, or removing database migrations. |
Managing EF Core Migrations
Overview
This project provides standardized scripts for all EF Core migration operations. Always use these scripts instead of running dotnet ef commands directly. The scripts ensure consistent project paths, output directories, and connection strings across all environments.
Available Scripts
Bash Scripts (Linux/macOS)
Located in scripts/bash/:
db-migrate-add.sh- Create a new migrationdb-migrate-list.sh- List all migrations and their statusdb-migrate-remove.sh- Remove the last migration (if not applied)db-update.sh- Apply all pending migrations to the databasedb-reset.sh- Drop and recreate the database with all migrations
PowerShell Scripts (Windows/Cross-platform)
Located in scripts/powershell/:
db-migrate-add.ps1- Create a new migrationdb-migrate-list.ps1- List all migrations and their statusdb-migrate-remove.ps1- Remove the last migration (if not applied)db-update.ps1- Apply all pending migrations to the databasedb-reset.ps1- Drop and recreate the database with all migrations
Common Workflows
1. Creating a New Migration
After modifying entity configurations or adding new entities:
Bash:
./scripts/bash/db-migrate-add.sh AddShowEntity
PowerShell:
./scripts/powershell/db-migrate-add.ps1 -MigrationName AddShowEntity
Migration Naming Conventions:
- Use PascalCase
- Start with a verb:
Add,Update,Remove,Rename - Be descriptive:
AddShowEntity,UpdateVenueAddressFields,RemoveObsoleteColumns
2. Applying Migrations
After creating a migration or pulling new migrations from source control:
Bash:
./scripts/bash/db-update.sh
PowerShell:
./scripts/powershell/db-update.ps1
3. Checking Migration Status
To see which migrations exist and which have been applied:
Bash:
./scripts/bash/db-migrate-list.sh
PowerShell:
./scripts/powershell/db-migrate-list.ps1
4. Removing a Migration
If you created a migration by mistake and haven't applied it yet:
Bash:
./scripts/bash/db-migrate-remove.sh
PowerShell:
./scripts/powershell/db-migrate-remove.ps1
Important: This only works if the migration hasn't been applied to any database. If it has been applied, you need to create a new migration to revert the changes.
5. Resetting the Database
To drop and recreate the database with all migrations (useful for development):
Bash:
./scripts/bash/db-reset.sh
PowerShell:
./scripts/powershell/db-reset.ps1
Warning: This destroys all data in the database. Only use in development environments.
Standard Workflow
When implementing persistence layer changes:
- Modify entity configurations in
src/GloboTicket.Infrastructure/Data/Configurations/ - Create migration using
db-migrate-add.shordb-migrate-add.ps1 - Review generated migration in
src/GloboTicket.Infrastructure/Data/Migrations/ - Apply migration using
db-update.shordb-update.ps1 - Verify schema by checking the database or running integration tests
Script Configuration
The scripts are pre-configured with:
- Project:
src/GloboTicket.Infrastructure - Startup Project:
src/GloboTicket.API - Output Directory:
Data/Migrations - Connection String:
Server=localhost,1433;Database=GloboTicket;User Id=migration_user;Password=Migration@Pass123;TrustServerCertificate=True;Encrypt=True
You should never need to specify these manually when using the scripts.
Why Use Scripts?
- Consistency: Everyone uses the same project paths and settings
- Safety: Connection strings and output directories are standardized
- Simplicity: No need to remember complex
dotnet efcommand syntax - Cross-platform: Bash and PowerShell versions provide identical functionality
- Error Handling: Scripts include proper error checking and user feedback
Troubleshooting
"No migrations configuration type was found"
- Ensure you're running the script from the project root directory
- The scripts expect to be run from the workspace root (not from subdirectories)
"The migration has already been applied to the database"
- You cannot remove a migration that has been applied
- Create a new migration to revert the changes instead
"Build failed"
- Fix compilation errors before creating migrations
- EF Core needs a successful build to generate migrations
"Unable to connect to the database"
- Ensure Docker containers are running:
./scripts/bash/docker-up.sh - Check database status:
./scripts/bash/docker-status.sh