EnsureCreated() and Migrate() are a pretty confusing combination
See original GitHub issueThe documentation states the following
/// <summary>
/// Ensures that the database for the context exists. If it exists, no action is taken. If it does not
/// exist then the database and all its schema are created. If the database exists, then no effort is made
/// to ensure it is compatible with the model for this context.
/// </summary>
/// <returns>
/// True if the database is created, false if it already existed.
/// </returns>
public virtual bool EnsureCreated()
In my thinking this lead to the following code
if (db.Database.EnsureCreated())
{
log.Info("Database created successfully.");
}
else
{
db.Database.Migrate();
log.Info("Database migrated successfully.");
}
And in THEORY this works, but it does not because
EnsureCreated()
does not do a Migration and populate the __EFMigrationsHistory
table.
This will lead to all failing consecutive calls to Migrate()
Now this leaves us with two options
- Successfully Create and Migrate a (non)existing database
- Successfully Create a database with non-working migrations
Is this really the intended behavior?
1.0.0-rc1-final + MSSQL Server
Issue Analytics
- State:
- Created 8 years ago
- Reactions:8
- Comments:5 (3 by maintainers)
Top Results From Across the Web
EnsureCreated() and Migrate() are a pretty confusing ...
EnsureCreated() is an alternative that completely skips the migrations pipeline and just creates a database that matches you current model. It's ...
Read more >EF7 EnsureCreated vs. Migrate Methods
Create(), the database will get created along with the migrations in the MigrationHistory table. With EF7 if you use EnsureCreated to create ...
Read more >Where should I put Database.EnsureCreated?
I am configuring EF as so: serviceCollection.AddEntityFramework() .AddSqlServer() .AddDbContext<Models.MyContext>(options => options.
Read more >Create and Drop APIs - EF Core
The EnsureCreated() and EnsureDeleted() methods provide a lightweight alternative to Migrations for managing the database schema.
Read more >Migrate wpf to blazor. Add the Razor SDK, Microsoft. Hot ...
Migrate (), we can, and want to, have the DbContext resolve automatically in Startup. ... It's not to be confused with Blazor WebAssembly...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You would either call
EnsureCreated()
orMigrate()
.EnsureCreated()
is an alternative that completely skips the migrations pipeline and just creates a database that matches you current model. It’s good for unit testing or very early prototyping, when you are happy just to delete and re-create the database when the model changes.You can just replace all your code with
db.Database.Migrate();
Discussed in triage and decided to leave these as they are. The use case for each will be well covered in the documentation.