question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

EnsureCreated() and Migrate() are a pretty confusing combination

See original GitHub issue

The 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:closed
  • Created 8 years ago
  • Reactions:8
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
rowanmillercommented, Nov 24, 2015

You would either call EnsureCreated() or Migrate(). 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();

1reaction
rowanmillercommented, Dec 1, 2015

Discussed in triage and decided to leave these as they are. The use case for each will be well covered in the documentation.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found