Adding Entity Framework Core 5 migrations to .NET 5 project

Database migrations help a developer to keep database schema up-to-date with the code. It is a core mechanism, that keeps changes in code and applies them in the database. Entity Framework Core 5 migrations are designed to keep track of DbContext class and generate migrations when you update it.

Installing the tools

To add EF Core migrations, you need to have Entity Framework Core already set up in your project. You can check how to go through that process in this post: PrimeHotel – adding Entity Framework Core 5 in .NET

The easiest way to add and manage migrations is to use .NET Core CLI tools, which you should have already installed. Type this command to check it out:

dotnet tool install --global dotnet-ef

You can also update the tool, once installed:

Adding a migration

Adding first migration doesn’t differ much from adding the next ones. You need to open a terminal window in the location of the project and execute the command:

dotnet ef migrations add InitialCreate

When this command executes successfully, it will generate a Migrations directory. InitialCreate file represents individual migration to match the DbContext. PrimeDbContextModelSnapshot represents the current state of the model. It is added to the project when the first migration is created and updated with each subsequent migration. It enables the migrations framework to calculate the changes required to bring the database up to date with the model.

In the InitialCreate file you will find two methods: Up and Down. Those will represent the changes when migration will be applied and when it would be rolled back.

Generated migration will stay the way it is. It’s not an auto-generated file that will be updated later in the process. This migration was generated for you to have a look and check if it does what it should. You can modify the migration according to your needs, nothing stops you from making some improvements.

Adding second and next migrations is a very similar process to the one presented above. You just need to use the command dotnet migrations add <name> add next migration will be generated.

Applying migrations manually

At this point, you can run your Entity Framework Core 5 migrations and update the database schema. You can do this via the following command:

dotnet ef database update

Database migrations will be applied and all executed migrations will be noted in the __EFMigrationsHistory. Here is the listing of this table after a couple of migrations.

Applying migrations automatically

It would be great if our changes were checked and applied on every project run. Let’s see how we can accomplish that. First of all, let’s go to Startup.cs file and create a method. 

    private void UpgradeDatabase(IApplicationBuilder app)
    {
        using (var serviceScope = app.ApplicationServices.CreateScope())
        {
            var context = serviceScope.ServiceProvider.GetService<PrimeDbContext>();
            if (context != null && context.Database != null)
            {
                context.Database.Migrate();
            }
        }
    }

This method will use built-in Dependency Injection mechanism to fetch an instance of our PrimeDbContext and use it to run database migrations. Only ones that were not applied yet will be run.

Now, in the Configure method add a line at the bottom.

    UpgradeDatabase(app);

With this mechanism set-up, the application will update the database it’s using, whether it is run locally, or deployed and run on a production server. 

Summary

Database migrations will help you keep database schema updated accordingly to the code changes. Adding an Entity Framework Core 5 migrations is a natural step when you have Entity Framework Core in place. All operations can be accomplished with .NET Core CLI tools and very simple commands. Remember that you can always edit migrations before applying them.

All code posted here is available at my GitHub, so you can download it freely. Take a look also at this post on how to run it: PrimeHotel – how to run this project.

Thanks for reading and good luck 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *