Etienne MENOU

Web Developer

Entity Framework Core tools reference

posts

Entity Framework (EF) Core tools are available in two main forms: the .NET Core CLI tools (dotnet-ef) and the Package Manager Console (PMC) tools in Visual Studio. Both tools provide similar functionality but are used in different environments and have different workflows.

EF Core tools are essential for developers working with Entity Framework Core, providing powerful commands to manage database migrations, scaffold models, and maintain the database schema in sync with the application’s data model.

.NET Core CLI

The .NET Core CLI (dotnet-ef) offers the advantage of being cross-platform, working seamlessly on Windows, macOS, and Linux, making it ideal for diverse development environments. It is highly suitable for automation and scripting, allowing easy integration into CI/CD pipelines and batch operations. They provide IDE independence, enabling use with any text editor or IDE, and are particularly efficient for command-line operations.

Installing dotnet-ef

dotnet ef can be installed as either a global or local tool. Most developers prefer installing dotnet ef as a global tool using the following command:

dotnet tool install --global dotnet-ef

Or updated with:

dotnet tool update --global dotnet-ef

Before you can use the tools on a specific project, you’ll need to add the Microsoft.EntityFrameworkCore.Design package to it:

dotnet add package Microsoft.EntityFrameworkCore.Design

To verify that EF Core CLI tools are correctly installed, you can use:

dotnet ef

Add a new migration

A migration allows you to incrementally update the database schema to keep it in sync with the application’s data model. Migrations provide a way to apply changes to the database schema over time as the data model evolves, without losing existing data. The following command allows you to add a new migration:

dotnet ef migrations add MigrationName

If the solution contains several projects, command line options must be used to specify locations:

  • --project <PROJECT> / -p: Relative path to the project folder of the target project. Default value is the current folder
  • --startup-project <PROJECT> / -s: Relative path to the project folder of the startup project. Default value is the current folder
  • --output-dir <PATH> / -o: The directory to put files in. Paths are relative to the project directory

For example:

dotnet ef migrations add NewMigration -s ".\src\API" -p ".\src\Infrastructure" -o "Data/Migrations"

This command adds a new migration named NewMigration to the project located in .\src\Infrastructure, using the startup project located in .\src\API. The migration files will be placed in the Data/Migrations directory within the .\src\Infrastructure project.

Update the Database

To apply the pending migrations to the database, and update the schema to match the current data model, you have to use the update command:

dotnet ef database update

As for the migration and regarding to your project, options can also be used:

dotnet ef database update -s ".\src\API" -p ".\src\Infrastructure"

The migration state is saved in a special tabgitle within the database called __EFMigrationsHistory. This table keeps track of all the migrations that have been applied to the database, ensuring that the database schema is in sync with the application’s data model.

Update an existing migration

When changes require updating an existing migration, you have to remove it and then create a new correct one:

dotnet ef migrations remove

You can remove multiple migrations in Entity Framework Core, but you need to do it one at a time.

If the migration to remove has already been applied to the database, it will return the following error: The migration '...' has already been applied to the database. Revert it and try again.[...]. To revert to a previous migration, you need to update the database to a specific migration. This process involves applying all the migrations up to and including the specified migration, effectively rolling back any migrations that were applied after it. List all migrations to find the targeted one:

dotnet ef migrations list

Then revert to the migration to update and remove it:

dotnet ef database update MigrationToUpadate
dotnet ef migrations remove

Package Manager Console in Visual Studio

When using Visual Studio, consider using the Package Manager Console tools instead of the CLI tools. Package Manager Console tools for Entity Framwork automatically:

  • Works with the current project selected in the Package Manager Console without requiring that you manually switch directories
  • Opens files generated by a command after the command is completed
  • Provides tab completion of commands, parameters, project names, context types, and migration names

Installing The Package Manager Console tools for Entity Framework Core

Install or update the Package Manager Console tools by running the following command in Package Manager Console:

Install-Package Microsoft.EntityFrameworkCore.Tools
Update-Package Microsoft.EntityFrameworkCore.Tools

Using the tools

It is similar to the dotnet-ef tool.

  1. Add a migration:
Add-Migration <MigrationName>
  1. Update the database:
Update-Database [-Migration <MigrationName>]
  1. Remove a migration:
Remove-Migration

2024 - Astro - modern-resume-theme