Entity Framework Core Interview Question #86
What is EnsureCreated and how is it different from migrations?
Migrations, Database Schema & Data Management Mid-Level Intermediate
Quick Interview Answer
EnsureCreated creates a database schema directly from the current EF Core model when the database does not exist. It does not establish the normal migrations history used for incremental schema evolution.
Detailed Explanation
EnsureCreated creates a database schema directly from the current EF Core model when the database does not exist. It does not establish the normal migrations history used for incremental schema evolution.
It is useful for transient tests, prototypes, or disposable databases where migration history is unnecessary.
For a long-lived relational production database that will evolve over time, migrations are generally the appropriate mechanism. Mixing EnsureCreated with a migrations-based lifecycle can cause problems.
Code Example
await db.Database.EnsureCreatedAsync();
// For a migrations-managed production database,
// use the migrations workflow instead.