ASP.NET Core Interview Question #220

How should database migrations be handled during deployment?

Testing, Deployment & Hosting Senior Advanced

Quick Interview Answer

Database schema changes must be coordinated with application deployment. EF Core migrations provide a versioned representation of schema changes, but the method used to apply them should match the production operating model.

Detailed Explanation

Database schema changes must be coordinated with application deployment. EF Core migrations provide a versioned representation of schema changes, but the method used to apply them should match the production operating model.

For controlled environments, migrations can be reviewed and applied through a deployment step or generated SQL script. Automatically migrating on every application startup may create concurrency, permission, rollback, and operational risks in larger systems.

Backward-compatible database changes can help support rolling or zero-downtime deployments.

Code Example

# Review migrations
dotnet ef migrations list

# Generate a deployment SQL script
dotnet ef migrations script --idempotent -o migration.sql

# Or apply in an approved deployment step
dotnet ef database update