Skip to content

Auto Apply Migrations

Apply any pending EF Core migrations automatically at application startup.

Add the following code in Program.cs, after var app = builder.Build(); and before app.Run();:

using (var scope = app.Services.CreateScope())
{
    var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
    await db.Database.MigrateAsync();
}

See Also