Introduction to Entity Framework Core Migrations

Entity Framework Core (EF Core) Migrations is a powerful feature that enables you to incrementally update your database schema to keep it synchronized with your object model. This process allows you to evolve your data models over time without losing existing data. Migrations are code-based, meaning you define your schema changes in C# code, which provides a robust and version-controlled approach to database schema management.

Key benefits of using EF Core Migrations include:

  • Version Control: Migrations are C# files, allowing them to be checked into your source control system alongside your application code.
  • Automation: Migrations can be applied automatically during deployment or manually through the .NET CLI or Package Manager Console.
  • Reproducibility: You can recreate your database schema at any point in its history.
  • Data Preservation: Migrations are designed to preserve existing data whenever possible.

Creating Migrations

When you make changes to your EF Core model (e.g., adding a new entity, property, or relationship), you need to generate a new migration to reflect these changes in your database. You can do this using the .NET CLI or the Package Manager Console in Visual Studio.

Using the .NET CLI:

Navigate to your project directory in the terminal and run the following command:

dotnet ef migrations add InitialCreate

Replace InitialCreate with a descriptive name for your migration. EF Core will analyze your model and generate two files:

  • A file containing the Up method (to apply the changes)
  • A file containing the Down method (to revert the changes)

Using the Package Manager Console (Visual Studio):

In Visual Studio, open the Package Manager Console and execute:

Add-Migration InitialCreate

The migration files will be created in the Migrations folder of your project.

Tip: Always provide descriptive names for your migrations. This makes it easier to understand the history of your database schema changes.

Applying Migrations

Once you have generated a migration, you need to apply it to your database. This will execute the C# code in the Up method of your migration files.

Using the .NET CLI:

dotnet ef database update

This command applies all pending migrations to the database. You can also specify a specific migration to update to:

dotnet ef database update MigrationName

Using the Package Manager Console (Visual Studio):

Update-Database

To update to a specific migration:

Update-Database MigrationName

Generating SQL Scripts

Sometimes, you might need to generate the SQL scripts that EF Core will execute without actually applying them to the database. This is useful for manual database deployments or for review purposes.

Using the .NET CLI:

dotnet ef migrations script

This command generates SQL scripts for all pending migrations. You can also specify a target migration:

dotnet ef migrations script MigrationName

To generate a script that includes commands to revert the migrations:

dotnet ef migrations script --from-migration <PreviousMigrationName> --to-migration <CurrentMigrationName> --idempotent

Using the Package Manager Console (Visual Studio):

Script-Migration

To script up to a specific migration:

Script-Migration MigrationName

Reverting Migrations

If you need to undo a migration, you can use the Down method defined in your migration files. This will revert the database schema changes made by that migration.

Using the .NET CLI:

dotnet ef database update <PreviousMigrationName>

Replace MigrationName with the name of the migration you want to revert to.

Using the Package Manager Console (Visual Studio):

Update-Database <PreviousMigrationName>

Advanced Migration Concepts

EF Core Migrations offers several advanced features:

  • Data Seeding: You can populate your database with initial data using the HasData method in your model configuration.
  • Custom Migrations: For complex database operations that cannot be expressed through EF Core's built-in operations, you can write custom SQL using migrationBuilder.Sql(...).
  • Complex Type Migrations: Handling migrations for complex types requires careful consideration and often manual intervention.
  • Table Splitting: EF Core supports mapping an entity to multiple database tables. Migrations can be generated for these configurations.

For more in-depth information on these advanced topics, please refer to the official EF Core Migrations documentation.