Skip to main content

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:
  • InitialCreate for the DataContext (tournament data)
  • CreateIdentitySchema for 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

Only remove a migration if it hasn’t been applied to any database yet. If the migration has been applied, you must roll it back first.
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
Using dotnet ef database update 0 will remove all tables from the database. This operation is destructive and will result in data loss.

Common Issues and Solutions

Issue: “No executable found matching command dotnet-ef”

Solution: Install the EF Core tools globally:
Verify installation:

Issue: “Build failed”

Solution: Build the solution before creating migrations:
Then add the --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 the DATABASE_CONNECTION_STRING environment variable is set:
Or update the OnConfiguring method in DataContext.cs to use a hardcoded connection string for development.

Issue: Foreign Key Constraint Violations

Solution: The application uses DeleteBehavior.Restrict for all relationships. When deleting entities, you must:
  1. First delete or update all dependent entities
  2. Then delete the parent entity
Example:
Delete Team with Dependencies

Issue: Migration Conflicts in Docker Build

Solution: If migrations already exist, the Docker build will fail. Either:
  1. Remove existing migrations before building:
  2. 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/
Each migration consists of three files:
  1. [Timestamp]_[MigrationName].cs - Contains Up() and Down() methods
  2. [Timestamp]_[MigrationName].Designer.cs - Metadata for the migration
  3. [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

  1. Always review migrations before applying them to production databases
  2. Test migrations on a development database first
  3. Back up your database before applying migrations in production
  4. Use descriptive migration names that indicate what changed (e.g., AddPhoneNumberToDirector, CreatePartidoTable)
  5. Never modify applied migrations - create a new migration instead
  6. Keep migrations small - one logical change per migration
  7. Generate SQL scripts for production deployments instead of running migrations directly

Data Contexts

Learn about DataContext and IdentityDataContext

Domain Entities

Explore the domain model entities