Overview
Entity Framework Core migrations provide a way to incrementally update the database schema to keep it in sync with the application’s data model while preserving existing data. The Tournament Management App uses migrations for two separate contexts:- DataContext: Tournament domain entities
- IdentityDataContext: ASP.NET Core Identity tables
Migration Workflow
The standard workflow for working with migrations involves these steps:1
Install EF Core Tools
Install the
dotnet-ef global tool:2
Make Model Changes
Modify entity classes in the
Torneo.App.Dominio project or update the DataContext/IdentityDataContext configuration.3
Create Migration
Generate a new migration file that captures the model changes:
4
Review Migration
Examine the generated migration file in the
Migrations folder to ensure it correctly represents your changes.5
Apply Migration
Update the database schema:
Docker Build Migrations
The application’s Dockerfile includes automated migration creation and execution during the build process. This ensures a fresh database schema is always available in the container.Build Process
Dockerfile (lines 19-35)
The Docker build process creates two migrations:
InitialCreatefor the DataContext (tournament data)CreateIdentitySchemafor the IdentityDataContext (user authentication)
Migration Commands Reference
Creating Migrations
DataContext Migration
Create DataContext Migration
IdentityDataContext Migration
Create IdentityDataContext Migration
Applying Migrations
Update to Latest Migration
Update DataContext
Update IdentityDataContext
Update to Specific Migration
Target Specific Migration
Listing Migrations
List DataContext Migrations
Removing Migrations
Remove Last Migration
Generating SQL Scripts
Generate SQL scripts without applying them to the database:Generate SQL Script
Rollback Procedures
To rollback the database to a previous migration:1
List Available Migrations
View all migrations to identify the target migration:
2
Update to Target Migration
Rollback to a specific migration:
3
Remove Unwanted Migrations
After rolling back, remove the migration files:
Rollback to Empty Database
To completely reset the database:Rollback All Migrations
Common Issues and Solutions
Issue: “No executable found matching command dotnet-ef”
Solution: Install the EF Core tools globally:Issue: “Build failed”
Solution: Build the solution before creating migrations:--no-build flag to the migration command:
Issue: “Your startup project doesn’t reference Microsoft.EntityFrameworkCore.Design”
Solution: Ensure the startup project (Torneo.App.Frontend) has the following package reference:
Torneo.App.Frontend.csproj
Issue: “Unable to create an object of type ‘DataContext’”
Solution: Ensure theDATABASE_CONNECTION_STRING environment variable is set:
OnConfiguring method in DataContext.cs to use a hardcoded connection string for development.
Issue: Foreign Key Constraint Violations
Solution: The application usesDeleteBehavior.Restrict for all relationships. When deleting entities, you must:
- First delete or update all dependent entities
- Then delete the parent entity
Delete Team with Dependencies
Issue: Migration Conflicts in Docker Build
Solution: If migrations already exist, the Docker build will fail. Either:-
Remove existing migrations before building:
- Modify the Dockerfile to check for existing migrations before creating new ones
Migration Files Structure
Migration files are generated in the following locations:- DataContext migrations:
Torneo.App.Persistencia/Migrations/ - IdentityDataContext migrations:
Torneo.App.Frontend/Migrations/
[Timestamp]_[MigrationName].cs- ContainsUp()andDown()methods[Timestamp]_[MigrationName].Designer.cs- Metadata for the migration[ContextName]ModelSnapshot.cs- Current state of the entire model
Example Migration Structure
Migration Up Method
Environment Variables
The following environment variables affect migration behavior:string
default:"Data Source=/app/Torneo.db"
SQLite connection string. Override to use a different database location or SQL Server.
string
SQL Server SA password. Only required if using SQL Server instead of SQLite.
Best Practices
- Always review migrations before applying them to production databases
- Test migrations on a development database first
- Back up your database before applying migrations in production
- Use descriptive migration names that indicate what changed (e.g.,
AddPhoneNumberToDirector,CreatePartidoTable) - Never modify applied migrations - create a new migration instead
- Keep migrations small - one logical change per migration
- Generate SQL scripts for production deployments instead of running migrations directly
Related Documentation
Data Contexts
Learn about DataContext and IdentityDataContext
Domain Entities
Explore the domain model entities