Entity Framework Core Interview Question #85
Should an ASP.NET Core application automatically call Database.Migrate at startup?
Migrations, Database Schema & Data Management Mid-Level Intermediate
Quick Interview Answer
Calling Database.Migrate at startup can be convenient for simple or tightly controlled deployments, but it is not universally appropriate.
Detailed Explanation
Calling Database.Migrate at startup can be convenient for simple or tightly controlled deployments, but it is not universally appropriate.
In multi-instance production systems, several instances may start together, application credentials may intentionally lack DDL permissions, and long schema operations can delay or disrupt startup. Schema changes may also require review or coordination with old and new application versions.
For larger production systems, applying reviewed migrations as a dedicated deployment step is generally easier to control and audit.
Code Example
// Convenient in controlled environments,
// but evaluate carefully for production architecture.
using var scope = app.Services.CreateScope();
var db = scope.ServiceProvider
.GetRequiredService<AppDbContext>();
await db.Database.MigrateAsync();