EF Core Migrations: A Detailed Guide

Managing database schemas as your applications grow can quickly become a headache. Manual changes are error-prone and time-consuming, leading to inconsistencies between development and production environments. I've experienced these issues on countless projects, and it's not pretty. How can we do better?


Enter Entity Framework (EF) Migrations, a powerful tool that allows you to version your database schemas.


Imagine this: Instead of writing SQL scripts, you define your changes in code. Need to add a column? Rename a table? No problem—EF Migrations has you covered. It tracks every modification to the data model, letting you review, test, and apply changes confidently, even across different environments.


In this Article, we'll break down the essentials of EF Migrations:

1. Creating Migrations: Learn how to define and generate migrations that capture your schema changes.

2. Migration SQL Scripts: Understand the SQL generated by your migrations and how to use it effectively.

3. Applying Migrations: Explore different methods to apply migrations to your database.

4. Migration Tools: Discover additional tools and frameworks for managing database migrations.

5. EF Migration Best Practices: I'll share my recommendations based on years of experience using EF.


We have many examples to cover, so let's dive in

Creating Migrations

If you're new to EF Migrations, I recommend checking out the EF Migrations documentation to understand the basics. For now, I'll assume you have some prior knowledge of EF Core.

Before we can create migrations with EF, we need an entity and a database context.

Let's define a simple `Customer` entity:


With this in place, we can start creating migrations to manage changes to our database schema.

We will also need a `DbContext` implementation, so let's define the `AppDbContext` class. In the `OnModelCreating` method, we're going to configure the `Customer` entity.

In this setup:

- The `Customer` entity is mapped to the `Customers` table.

- A check constraint ensures that the `Email` field contains an `@` character.

- The `Id` property is configured as the primary key.

- The `Name` property has a maximum length of 100 characters.

- The `Email` property has a maximum length of 200 characters and is required.

- An index on the `Email` property ensures uniqueness.


Let's break down a few of the methods we're using:

- ToTable: Configures the table name for the specific entity. It also allows us to provide a `TableBuilder` delegate, which we can use to configure a check constraint using `HasCheckConstraint`.
- HasKey: Configures the table's primary key. EF will also pick up the `Id` property by convention, so this step is optional.
- Property: Represents the entry point for configuring individual properties of the entity.
- HasIndex: Defines an index on the specified property (or properties). We can also declare that the index should be unique by calling `IsUnique`.

We're now ready to create our first migration. Using the PowerShell syntax, you can run:

Add-Migration Create_Database

This command will create the first database migration called `Create_Database`. The migration will apply the configuration we defined in the `OnModelCreating` method. It contains the `Up` and `Down` methods, allowing us to apply or revert changes to the database.

Note that some operations are destructive (like removing a column) and can't be easily reverted. It's up to you to examine the generated migration and prevent any possible data loss.


Customizing Migrations

We can also modify the migration files if we need to apply some custom changes.

A notable example is renaming a column. Let's say we rename the `Description` property to `ShortDescription`. In some EF versions, this would result in the following migration:



What's the problem here? By calling `DropColumn` first, we will remove the column from the database and lose valuable data.

What we actually want to do is rename the existing column. So, we can modify the migration file to use the `RenameColumn` method:



Another example is executing custom SQL commands from your migrations. Custom SQL commands are helpful when we can't express something through the EF fluent API. I've used it in the past to migrate data from one column to another or define a complex index.


In this approach:
- Use the `RenameColumn` method to rename a column without losing data.
- Implement custom SQL commands in the `Up` method for changes that can't be expressed through the EF fluent API.
- Ensure that the `Down` method also reverts any custom changes.

Applying Migrations

How do we apply EF migrations to the database? 

Command-line Tools

The most common approach to applying database migrations is using the CLI. You can use either the `dotnet ef` tool or the PowerShell commands. For example, you can execute the `Update-Database` command from PowerShell to apply any pending migrations.

Update-Database -Migration <ToMigration> -Connection <ConnectionString>

This command applies the specified migration to the database using the provided connection string. If no migration is specified, it applies all pending migrations.

 EF Core Migrations Best Practices

I want to wrap up this issue with a few tips from my experience working with EF Core migrations over the years:

1. Use Meaningful Migration Names: Avoid generic names or dates. Use clear, descriptive names that indicate the purpose of the migration, such as `AddProductsTable` or `RenameDescriptionToShortDescription`. This makes it much easier to understand your migration history and locate specific changes.

2. Keep Migrations Small and Focused: Avoid creating large migrations with multiple unrelated changes. Smaller migrations are easier to review, test, and troubleshoot. Aim for one migration per feature or logical change.

3. Test Migrations Thoroughly: Before applying migrations to production, test them in a development or staging environment. These environments should mirror your production setup as closely as possible to catch any unexpected issues or data loss risks before they impact real users.

4. Beware of Destructive Changes: Operations like dropping columns or tables can lead to irreversible data loss. Carefully consider the consequences before including such changes in migrations. Provide a way to migrate data or create a backup plan.

5. Avoid Merge Conflicts: Solving merge conflicts for EF migration snapshots can be a real headache. Be mindful of this when working in a team that creates many database migrations. Always be up to date with the latest migration before creating a new one to minimize the chance of merge conflicts.

    My preferred approach to applying migrations is using SQL scripts. Depending on the project's scope and complexity, this can be done manually or through an automated tool. This allows for a thorough review of the migration and the identification of any potential problems.

I hope this was helpful!

And Keep Learning till the Next Time

Assalamualaikum - Allahuma Barik
Peace Out✌️


Post a Comment

0Comments
Post a Comment (0)