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]

PARAMETERS

--assembly
    The assembly to use. This is usually the project's output assembly.

--startup-assembly
    The startup assembly to use. This can be different from the assembly when using a separate startup project.

--project
    The project to use. This defaults to the current directory.

--context
    The DbContext to use. By default, the command will search for the DbContext in the project.

--configuration
    The configuration to use when building the project (e.g., Debug or Release).

--framework
    The target framework to use (e.g., net6.0).

--runtime
    The target runtime to use (e.g., linux-x64). This parameter is useful when publishing self-contained applications.

--verbose
    Show verbose output.

--no-color
    Don't colorize output.

--prefix-output
    Prefix output with the project name.

--help
    Show command line help.

--version
    Show version information.

DESCRIPTION

The dotnet-ef command is a .NET CLI tool that provides design-time services for Entity Framework (EF) Core.

It allows developers to create and manage databases and EF Core models from the command line. You can use it to perform tasks like creating migrations, updating databases, scaffolding database contexts and entity types from an existing database. dotnet-ef simplifies database development with EF Core and is heavily used in conjunction with `dotnet build` command.

It must be installed as a global or local tool using the `dotnet tool install` command. The dotnet-ef command is especially useful in development, testing, and continuous integration/continuous deployment (CI/CD) environments. If you want to connect to your database you need to have the correct provider installed. After installing the provider and the ef tool, you can run all the commands like `dotnet ef database update`, `dotnet ef migrations add`, etc.

CAVEATS

Requires the .NET SDK to be installed.
The command must be used within a .NET project directory or specify the project path using `--project`.
Ensure the correct EF Core provider package is installed for your database.

COMMON SUBCOMMANDS

dotnet ef migrations add: Adds a new migration.
dotnet ef database update: Updates the database to the latest migration.
dotnet ef database drop: Drops the database.
dotnet ef dbcontext scaffold: Scaffolds a DbContext and entity types from an existing database.

HISTORY

The dotnet-ef command was introduced with the adoption of the .NET Core CLI.

Before that, similar functionalities were offered through tools tied to Visual Studio.

Its introduction marked a shift toward cross-platform, command-line-centric development for EF Core. The tool is continuously evolving with new EF Core releases.

SEE ALSO

Copied to clipboard