Entity Framework Core (EF Core) migrations provide a way to incrementally apply schema changes to your database, keeping it in sync with your application's data model. Migrations allow you to evolve your database schema over time as your application requirements change. This tutorial covers everything from creating and applying migrations to managing them effectively.
Migrations in EF Core are a series of operations that apply schema changes to a database. They allow developers to:
To create a new migration, use the following command. This command adds a migration with the specified name and captures the current state of the data model.
dotnet ef migrations add InitialCreate
This creates a migration file in your project that includes the operations to apply the changes.
After creating a migration, apply it to the database using the following command. This command updates the database to match the model's current state.
dotnet ef database update
EF Core will execute the operations defined in the migration file, modifying the database schema accordingly.
If you need to revert a migration, you can roll back to a previous state using the following command. Specify the target migration to revert to.
dotnet ef migrations remove
This command reverts the schema changes applied by the specified migration, allowing you to undo mistakes or incorrect changes.
EF Core provides several commands to help manage migrations effectively:
dotnet ef migrations list
to see all applied migrations.dotnet ef migrations remove
to delete the most recent migration.dotnet ef migrations script
to generate a SQL script for the migration.
If your application uses multiple DbContexts, you can specify which context to use when creating or applying migrations. Use the -c
or --context
option to specify the context.
dotnet ef migrations add InitialCreate -c YourDbContextName
This command creates a migration for the specified DbContext, ensuring that changes are applied to the correct part of your application.
Visual Studio provides a Package Manager Console for managing EF Core migrations. You can run commands directly within Visual Studio:
Add-Migration InitialCreate
Update-Database
Remove-Migration
Using Visual Studio simplifies the process by integrating it into the development environment, providing output and feedback directly in the console.
EF Core CLI commands offer several options to customize their behavior:
-s
or --startup-project
: Specifies the startup project to use when executing the command. Useful for solutions with multiple projects.--verbose
: Provides detailed output for debugging and understanding the execution flow of the command.-o
or --output-dir
: Specifies the directory where the migration files should be saved.You can leverage advanced options to gain more control over migrations. For example, when working with specific contexts or projects:
# Add a migration for a specific DbContext with verbose output and specify a startup project
dotnet ef migrations add CompanyEmailHistory --context "ApplicationDbContext" --verbose -s ../WebAPI
# Update the database with verbose output and specify a startup project
dotnet ef database update --context "ApplicationDbContext" --verbose -s ../WebAPI
This example demonstrates how to use multiple command options to manage migrations for a specific DbContext in a multi-project solution.
Here are some best practices for managing migrations in EF Core:
EF Core migrations provide a powerful mechanism for managing database schema changes in a controlled and versioned manner. By following best practices and leveraging the tools available, developers can ensure smooth transitions between different versions of their applications.