EF Core - Migrations

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.


1. Understanding Migrations

Migrations in EF Core are a series of operations that apply schema changes to a database. They allow developers to:


2. Creating Migrations

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.


3. Applying Migrations

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.


4. Rolling Back Migrations

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.


5. Managing Migrations

EF Core provides several commands to help manage migrations effectively:



6. Using Migrations with Multiple DbContexts

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.


7. Using Visual Studio for Migrations

Visual Studio provides a Package Manager Console for managing EF Core migrations. You can run commands directly within Visual Studio:

Using Visual Studio simplifies the process by integrating it into the development environment, providing output and feedback directly in the console.


8. Understanding Command Options

EF Core CLI commands offer several options to customize their behavior:


9. Advanced Command Usage

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.


10. Best Practices for Migrations

Here are some best practices for managing migrations in EF Core:


Summary

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.