DEV Community

Daniel Leinweber
Daniel Leinweber

Posted on

How to use EF Core Migrations

The EF Core Migrations feature provides a way to incrementally update the database schema to keep in sync with your application's data model, while still preserving existing data in your database.

In this article I want to give you an overview of the most common tasks you might face when working with Migrations in EF Core.

Content

Add a migration

After you have created your models and your EF Core context, or after you have made changes to them, you can add a migration as follows.

dotnet ef migrations add "Add_Created_Column_To_Customers"
Enter fullscreen mode Exit fullscreen mode

Tip: Try to name your migration like a Git commit message

Customize migration code

Sometimes it might be necessary to customize a migration. For this you can update the Up or Down method in the migration file.

Column renames

To rename a property from Name to FullName, you need to replace the generated code

migrationsBuilder.DropColumn(
    name: "Name",
    table: "Customers"
);

migrationsBuilder.AddColumn<string>(
    name: "FullName",
    table: "Customers",
    nullable: true
);
Enter fullscreen mode Exit fullscreen mode

with the following. This is due to EF Core not knowing if you intended to drop and create a new column or to rename a column.

migrationsBuilder.RenameColumn(
    name: "Name",
    table: "Customers",
    newName: "FullName"
);
Enter fullscreen mode Exit fullscreen mode

Tip: If you rename your property in your model with the refactoring tools of Visual Studio (e.g. F2) EF Core is usually smart enough to recognise that you meant to rename the property. So you just need to check if the generated code is already correct.

Adding raw SQL

Sometimes you might want to execute specific operations that cannot be auto generated by EF Core.

If you want to rename and combine a FirstName and LastName property into a single FullName property, you need to replace the generated code

migrationBuilder.DropColumn(
    name: "FirstName",
    table: "Customers"
);

migrationBuilder.DropColumn(
    name: "LastName",
    table: "Customers"
);

migrationBuilder.AddColumn<string>(
    name: "FullName",
    table: "Customers",
    nullable: true
);
Enter fullscreen mode Exit fullscreen mode

with the following, in order to prevent unwanted data loss.

migrationBuilder.AddColumn<string>(
    name: "FullName",
    table: "Customer",
    nullable: true
);

migrationBuilder.Sql(
@"
    UPDATE Customers
        SET FullName = FirstName + ' ' + LastName;
"
);

migrationBuilder.DropColumn(
    name: "FirstName",
    table: "Customers"
);

migrationBuilder.DropColumn(
    name: "LastName",
    table: "Customers"
);
Enter fullscreen mode Exit fullscreen mode

Arbitrary changes via raw SQL

Raw SQL can also be used to manage database objects that EF Core is not aware of. You might add a migration without making any model change to generate an empty migration.

Then you can add the following to create a SQL Server stored procedure.

migrationBuilder.Sql(
@"
    EXEC (
        'CREATE PROCEDURE sp_GetFullName
            @LastName nvarchar(50),
            @FirstName nvarchar(50)
        AS
            RETURN @LastName + @FirstName;'
    )
"
);
Enter fullscreen mode Exit fullscreen mode

Remove a migration

To remove the last migration, use this command.

 dotnet ef migrations remove
Enter fullscreen mode Exit fullscreen mode

Execute migrations

To create or update the database and schema, use this command.

dotnet ef database update
Enter fullscreen mode Exit fullscreen mode

Please be aware that the recommended way to deploy migrations to a production database is by generating SQL scripts.

Basic Usage

Create a SQL script from a blank database to the latest migration:

dotnet ef migrations script
Enter fullscreen mode Exit fullscreen mode

With From

Create a SQL script from the given migration to the latest migration:

dotnet ef migrations script AddNewTables
Enter fullscreen mode Exit fullscreen mode

With From and To

Create a SQL script from a specific migration to a specific migration:

dotnet ef migrations script AddNewTables AddAuditTable
Enter fullscreen mode Exit fullscreen mode

You can use a from that is newer than the to in order to generate a rollback script.

Top comments (0)