Skip to content

CheckMigrated #24866

Open

Description

@ajcvickers

One of the dilemmas of "getting started" applications and examples is that creating the database in code when the application starts is by far the easiest way to get up and running with a database, but this is considered an anti-pattern for most production applications. So, even though its a bit harder, we want people to start with something like Migrations which allows us to show best practice from the start. However, using Migrations requires running some commands at the command line.

The idea behind EnsureMigrated is to have a single, simple line of code that can safely be run when starting an application and which will check if Migrations have been created and applied to the database. If no migrations are found in the project, then EnsureMigrated will stop and indicate that the user should run dotnet ef migrations add. For example:

A database must be created for this application. Enter the following commands to create a database using EF Core Migrations:
dotnet ef migrations add InitialMigration
dotnet ef database update
See https://aka.ms/databasemigrations for more information.

We can potentially also detect other conditions such as a migration exists, but update database has not been called. We may also want to check if the dotnet-ef tool is installed and give instructions for that.

To make this easier, we could create a new command that will add a migration and update the database in one go. For example:

A database must be created for this application. Enter the following commands to create a database using EF Core Migrations:
dotnet ef database initialize
See https://aka.ms/databasemigrations for more information.

The end-result is guidance towards best practice with minimal input from the user. It's also not magic--that is, the user knows that they are creating a database.

Thoughts @davidfowl @bricelam @JeremyLikness @halter73 @LadyNaggaga @glennc

Activity

  1. DamianEdwards commented on May 28, 2021

    @DamianEdwards
    Member

    This looks good @ajcvickers. What do you think we should do with regards to actually calling EnsureCreated then? Should it be our templates that use EF? Would we update the database exception filter for the developer error page to show the content you propose, along with details on how and where to call EnsureCreated? Of course getting the database exception filter registered is itself a manual task today which presents its own challenges WRT discovery (separate package, separate call to add the filter to services, etc.).

  2. bricelam commented on Jun 2, 2021

    @bricelam
    Contributor

    This could leverage the API proposed in #22105 (comment) to check for unscaffolded migrations.

  3. bricelam commented on Jun 2, 2021

    @bricelam
    Contributor

    📝 Design meeting notes

  4. ErikEJ commented on Jun 3, 2021

    @ErikEJ
    Contributor

    "VerifyMigrated" ?

  5. added this to the Backlog milestone on Jun 8, 2021
  6. modified the milestones: Backlog, 7.0.0 on Oct 20, 2021
  7. modified the milestones: 7.0.0, Backlog on Apr 23, 2022
  8. vigouredelaruse commented on Sep 28, 2022

    @vigouredelaruse
  9. removed their assignment
    on Jul 8, 2023
  10. changed the title [-]EnsureMigrated[/-] [+]CheckMigrated[/+] on May 16, 2024
  11. removed their assignment
    on Aug 31, 2024
  12. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions