liveBook · Manning

Dotnet ef database update

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

dotnet ef database update is an Entity Framework Core command that creates the database when necessary and applies pending migrations.

dotnet ef database update creates the database when necessary and applies every migration that has not yet been applied. EF Core then brings the database schema into line with the application's data model. section 12.3.1 section 12.6

Run the command from the application project folder, then expect EF Core to:

  • Create the necessary tables, columns, and keys. section 12.3.1
  • Record applied migration names in the __EFMigrationsHistory table. section 12.3.1
  • Apply only newly added migrations when the command runs again. section 12.3.1
  • Report “No project was found” if run from the top-level solution folder instead of the application project folder. section 12.3.1

Apply the initial schema

After creating a migration such as dotnet ef migrations add InitialSchema, run dotnet ef database update. EF Core creates the database if needed and applies the migration; later runs apply only outstanding migrations. section 12.3.1 section 12.6

Figure 12.7 Applying migrations to a database creates the database if it doesn’t exist and updates the database to match EF Core’s internal data model. The list of applied migrations is stored in the __EFMigrationsHistory table.
CH12_F07_Lock3

Use the schema diagram to see how applying migrations creates the database and records applied migrations. section 12.3.1

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

How deep it goes
Explains database creation, schema changes, migration history, and project-folder errors without detailing generated SQL.
Examples
Uses InitialSchema, the update command, created tables, and __EFMigrationsHistory as concrete examples.
What it assumes
Assumes a correctly configured EF Core application and execution from its project folder.
What you can do after
Operate EF Core migrations by creating a migration and applying it with dotnet ef database update.
How it connects
Connects migrations with entity definitions, DbContext, database schema, and the __EFMigrationsHistory table.

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.