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 [options] [command] [arguments]

PARAMETERS

-h|--help
    Show help information

--version
    Show version information

-v|--verbose
    Show verbose output (+v, ++v, +++v for more detail)

--no-color
    Disable ANSI color output

--prefix-output
    Prefix output lines with log level

DESCRIPTION

dotnet ef is the command-line interface for Entity Framework Core (EF Core), Microsoft's ORM for .NET. It enables developers to manage database schemas, migrations, and model scaffolding efficiently.

Primary uses include:
• Creating, adding, and applying migrations to evolve database schemas from C# models (code-first).
• Scaffolding DbContext and entity classes from existing databases (database-first).
• Dropping, updating, or scripting database operations.

Integrated with the .NET SDK, it supports databases like SQL Server, PostgreSQL, MySQL, and SQLite. Typical workflow: design models, run dotnet ef migrations add InitialCreate, then dotnet ef database update. Verbose logging and color output aid debugging.

Essential for ASP.NET Core apps, it automates repetitive tasks, ensuring consistency between code and database. Requires specifying project, startup project, or connection strings via options.

CAVEATS

Requires global or local installation via dotnet tool install --global dotnet-ef. Specify --project or --startup-project for multi-project solutions. Subcommands have additional options; use --help for details.

SUBCOMMANDS

dbcontext: info, list, scaffold.
database: drop, update.
migrations: add, list, remove, script.

INSTALLATION

dotnet tool install --global dotnet-ef --version 8.0.0 (match EF Core version). Or dotnet tool install dotnet-ef for local use.

HISTORY

Stable release with EF Core 2.0 (2017); preview in EF Core 1.x. Evolved with .NET releases, adding support for new providers and features like design-time factories.

SEE ALSO

dotnet(1)

Copied to clipboard