LinuxCommandLibrary

dotnet-ef

Manage Entity Framework Core databases

TLDR

Update the database to a specified migration

$ dotnet ef database update [migration]
copy

Drop the database
$ dotnet ef database drop
copy

List available DbContext types
$ dotnet ef dbcontext list
copy

Generate code for a DbContext and entity types for a database
$ dotnet ef dbcontext scaffold [connection_string] [provider]
copy

Add a new migration
$ dotnet ef migrations add [name]
copy

Remove the last migration, rolling back the code changes that were done for the latest migration
$ dotnet ef migrations remove
copy

List available migrations
$ dotnet ef migrations list
copy

Generate an SQL script from migrations range
$ dotnet ef migrations script [from_migration] [to_migration]
copy

SYNOPSIS

dotnet ef <command> [options]
or
dotnet ef <command> <subcommand> [arguments] [options]

PARAMETERS

migrations
    Commands for managing Entity Framework Core migrations. This includes adding new migrations, applying them to a database, and removing them.

database
    Commands for managing the Entity Framework Core database. This includes updating the database to the latest migration, dropping it, and generating SQL scripts.

dbcontext
    Commands for managing Entity Framework Core DbContext types. This includes scaffolding a DbContext and entity types from an existing database, and listing available DbContext types.

--help
    Shows help information for the dotnet-ef tool or a specific command.

--version
    Shows the version of the dotnet-ef tool currently installed.

DESCRIPTION

The dotnet-ef command is the command-line interface (CLI) tool for Entity Framework Core (EF Core). It provides a wide range of commands to manage EF Core operations directly from the terminal, essential for database-first or code-first development workflows. Its primary functions include applying and creating database migrations, scaffolding database contexts and entity types from an existing database, and managing database connections. It's an indispensable tool for developers working with .NET applications that use EF Core for data access, allowing automation of common database schema evolution and reverse engineering tasks. The tool integrates seamlessly with the .NET SDK, functioning identically across Linux, Windows, and macOS environments.

CAVEATS

The dotnet-ef tool requires the .NET SDK to be installed on the system. It typically needs to be executed from the project directory containing the DbContext, or the project must be explicitly specified using options like -p or --project. It also relies on the Microsoft.EntityFrameworkCore.Design NuGet package being referenced by the project. Complex setups involving multiple DbContexts or specific build configurations might require additional command-line options or environment variables.

INSTALLATION

The dotnet-ef tool is a .NET Global Tool. It can be installed using the .NET CLI: dotnet tool install --global dotnet-ef. This makes it available across all your projects. Alternatively, it can be installed as a local tool within a specific project by adding a tool reference in the project's manifest file (.config/dotnet-tools.json).

KEY FEATURES

Key features include:
Migrations Management: Creating, applying, and removing database migrations, crucial for evolving database schemas with application code.
Database Management: Updating the database to a specific migration, dropping the database, and scripting database creation or migration steps.
Scaffolding: Generating DbContext and entity classes from an existing database, facilitating database-first development.
DbContext Debugging: Displaying information about the DbContext and its models, useful for troubleshooting and understanding the EF Core model.

HISTORY

The dotnet-ef command emerged as an essential component of Entity Framework Core (EF Core), which itself is a modern, cross-platform and open-source version of Microsoft's popular object-relational mapper (ORM). Unlike its predecessor, Entity Framework 6 (EF6), which primarily relied on PowerShell commands within Visual Studio's Package Manager Console, EF Core was designed from the ground up for cross-platform compatibility. The dotnet-ef tool was introduced alongside EF Core 1.0 to enable developers to manage database migrations, scaffold entities, and interact with the database schema directly from the command line, across Linux, Windows, and macOS. Its development has mirrored the evolution of .NET Core itself, becoming a standard global tool for .NET development.

SEE ALSO

dotnet(1)

Copied to clipboard