liveBook · Manning

Dotnet-ef

As explained in Building Web APIs with ASP.NET Core

dotnet-ef is the .NET command-line tool for Entity Framework Core design-time tasks, including creating, applying, and managing migrations.

dotnet-ef is the command-line tool for Entity Framework Core (EF Core) design-time work. In a board-game API, it can turn C# entities into database tables, keep those tables aligned with model changes, and prepare schema changes for deployment. section 4.3.2 section 4.3.5

A code-first setup follows this sequence:

  • Install the EF Core packages and dotnet-ef, then run `dotnet ef` to check that the tool is available. The `--global` installation switch makes it available across projects. section 4.3.2
  • Create the entity classes and `ApplicationDbContext`, and configure its database connection string. These give migrations a model and a database to work with. section 4.3.5 section 4.5
  • Run `dotnet ef migrations add Initial` from the project root to generate an initial migration. Check for the new `/Migrations/` folder: it contains the migration and a model snapshot used to calculate later changes. section 4.3.5
  • Run `dotnet ef database update Initial` to apply that migration. Adding a migration creates files; updating the database is the separate step that changes its structure. section 4.3.5

Code-first starts with C# entities and uses migrations to generate and update the schema. Database-first starts with an existing schema and uses scaffolding to generate entities. To roll back a later schema change, apply an earlier migration and inspect the database to confirm the unwanted tables or columns are gone. section 4.3.2 section 4.4.6

Preparing a production database

Run `dotnet ef migrations script -o ./Migrations/script.sql` on a development machine to generate a SQL script for the missing board-game tables. A DBA can review the script before it runs in production. A migration bundle is another option: it runs without requiring dotnet-ef on the production machine. section 12.4.4

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Walks through installation, migration creation, database updates, and rollback verification; compares production deployment options without detailing their execution.
Examples
Shows an initial migration, a database update, rollback inspection, and a SQL script generated for production.
What it assumes
Assumes a .NET project with EF Core entities, a DbContext, and a connection string; introduces modeling approaches and tool installation.
What you can do after
Operate/configure dotnet-ef to create and apply migrations, verify schema changes, and generate a production SQL script.
How it connects
Connects migrations and scaffolding to code-first and database-first modeling, DbContext, connection strings, and model snapshots.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.