Skip to main content
EF Core

.NET 6 - How To Run Automatic EF Core Migrations at Startup of Application

Christian Schou

Have you ever been thinking about upgrading your database automatically to the latest version when the application is booting up?

In this super simple and quick .NET 6 guide, I will show you how to apply migrations automatically to your database when the application is starting up. We will be using Entity Framework Core inside Program.cs to accomplish this simple but very helpful task.

What is Entity Framework Core?

Before we dig in, let's take a quick intro to Entity Framework Core. Entity Framework Core (EF Core) is a lightweight, extensible, open-source, and cross-platform version of the very popular Entity Framework.

GitHub - dotnet/efcore: EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations. - GitHub - dotnet/efcore: EF Core is a modern object-database mapper ...

Entity Framework core can serve as an object-relation mapper (ORM). What does that mean, Christian? Excellent question! Here is a quick explanation:

  • It will allow you to work with a database using only .NET objects. This is also what we refer to as the code-first approach.
  • It will take away most of the data-access code you normally would have to write when you should interact with a database.

When using Entity Framework (EF) Core, you can use a lot of different database engines, like:

  • MS SQL
  • PostgreSQL
  • MySQL
  • Sqlite
  • InMemory

For a complete list of database providers you can use with EF Core, please see the below link.

Database Providers - EF Core
Information about specific supported Entity Framework Core providers and about providers in general

Run automatic migrations at the startup

Okay - let's get to it. I have divided this article into two sections. The first one will show you how to register your database context at startup, and the second will apply the automatic migrations.

Register Database Context

The first thing you have to do is register your Entity Framework Context using Dependency Injection (DI) inside Program.cs or any other IServiceCollection extension you reference at the startup of your application. We can register our database context by calling builder.Services.AddDbContext<TContextClass>().

Below is a code snippet you can copy and paste into your Program.cs file for registering your AppDbContext. Remember to rename AppDbContext to the name of your database context.

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddDbContext<AppDbContext>();

...

Okay, now our application will have a connection to the database when it is booting.

Perform automatic migrations

The last step in this tutorial is easy. We have already registered our database context at startup and we are now able to use it by using Dependency Injection.

Because we got dependency injection we can create a scope for the database service and migrate it. This is completed by calling scope.ServiceProvider.GetRequiredService<T>();. You can do this in Program.cs or any other file, where you inject your database context.


// Migrate latest database changes during startup
using (var scope = app.Services.CreateScope())
{
    var dbContext = scope.ServiceProvider
        .GetRequiredService<AppDbContext>();
    
    // Here is the migration executed
    dbContext.Database.Migrate();
}

That's it. When you start your application now it will automatically run migrations for you.

Summary

Migrations are a crucial part of the evolvement of an application. It's easy to forget about your changes and can be difficult for new developers to perform if they don't understand the dotnet cli syntax.

By using dependency injection we can easily create a scope for our application database context and migrate it to be compatible with the latest version of our domain model. Just remember to register the entities in your database context class.

I hope you learned something new in this short Entity Framework Core tutorial. If you got any issues, questions, or suggestions, please let me know in the comments below. Until next time - Happy coding! ✌️