Categories
Programming

.NET Core Entity Framework Migrations

When you don’t want to script out your own database, Entity Framework Core has your back. When you change your database context or the models it uses, you can use a couple of commands to create and update your database to keep it in sync with your model. These examples run in the Package Manager console (and if you have trouble running them, check the end of this post).

Your package manager console can be found in Visual Studio using Tools -> NuGet Package Manager -> Package Manager Console.

Package Manager Location

What Will Be Included

To make it into a migration, you need a model class to represent the table. You also need the property on the database context class.

Example model.

[Table("Content")]
public class Content
    : IDataModel
{
    public long Id { get; set; }

    public string Title { get; set; }

    public string CreatedBy { get; set; }
}

Example database context, with a DbSet of the model type.

public class ApplicationDbContext
    : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    public DbSet<Content> Contents { get; set; }
}

Create the Initial Version

To create the first migration file, run the dotnet ef migrations add command, and name the migration “InitialCreate”:

dotnet ef migrations add InitialCreate

You will find some new files in your project, named using a date-stamp and the name of your version. For example 20200328212129_InitialCreate.cs. You can view the file and see what it does, it will all look pretty familiar. There is an “Up” method and a “Down” method. This allows you to apply or reverse the migration.

To apply all migrations and get the database into the up-to-date state, run:

PM> dotnet ef database update
Done.

Applying Changes

Each time you have changes you want to push into the database, you run the ef migrations add command with an appropriate name. In the example below, the “Container” table is going to be added…

PM> dotnet ef migrations add AddContainers
Done. To undo this action, use 'ef migrations remove'

And you make it happen using the same command as before:

PM> dotnet ef database update
Done.

The common pattern is ef migrations add [name] -> ef database update.

Undo! Removing a Migration

If you made a mistake in adding a migration, you can remove it using this command:

PM> dotnet ef migrations remove
Removing migration '20200328212010_AddContainers'.
Reverting model snapshot.
Done.

Troubleshooting

The most likely error you’ll encounter is “Could not execute because the specified command or file was not found”.

When this happens, you just need to add the following to your .csproj file:

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />
  </ItemGroup>

Or you can just install the tool globally:

dotnet tool install --global dotnet-ef

To update the tool, use the following command, passing the appropriate version (you’ll be told the version when you get warned you are out of date).

dotnet tool update --global --version 3.1.5 dotnet-ef

If the tool can’t work out which project to use, you can give it a hint. All the commands accept the --project parameter.

dotnet ef migrations add InitialCreate --project Fenton.MigrationExample