liveBook · Manning

Dotnet ef migrations add

As explained in ASP.NET Core in Action, 3e

dotnet ef migrations add is an Entity Framework Core command that compares the current model with a prior snapshot and generates a new migration.

dotnet ef migrations add compares the current Entity Framework Core model with its previous snapshot and generates a migration for schema changes. It keeps entity definitions, EF Core’s internal model, and the database schema aligned. section 12.6

Use the command after changing entities:

  • Change the entity model, such as adding IsVegetarian and IsVegan to Recipe. section 12.3.2
  • Run dotnet ef migrations add ExtraRecipeFields in the project folder. section 12.3.2
  • Review the generated migration, its designer snapshot, and the updated AppDbContextModelSnapshot.cs. section 12.3.2
  • Apply the pending migration separately with dotnet ef database update; creating the migration alone does not change the database. section 12.6 section 12.3.2

Adding recipe flags

After adding IsVegetarian and IsVegan to Recipe, run dotnet ef migrations add ExtraRecipeFields. EF Core creates the migration files; dotnet ef database update then adds the fields to the Recipes table. section 12.3.2

Figure 12.8 Creating a second migration and applying it to the database using the command-line tools
CH12_F08_Lock3

Follow the command-line workflow from changing the entity to creating and applying the migration. section 12.3.2

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains model comparison, generated files, and deferred database updates, but stops short of detailed migration management.
Examples
Demonstrates adding IsVegetarian and IsVegan to Recipe and applying the resulting migration to the Recipes table.
What it assumes
Assumes an EF Core DbContext and entity model, while showing how changed entities become migration files.
What you can do after
Operate/configure EF Core migrations by generating a named migration and applying it separately to the database.
How it connects
Connects Entity Framework Core migrations with DbContext, model snapshots, database schema, and source control.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.