Entity Framework Core tools reference - .NET Core CLI
The command-line interface (CLI) tools for Entity Framework Core perform design-time development tasks. For example, they create migrations, apply migrations, and generate code for a model based on an existing database. The commands are an extension to the cross-platform dotnet command, which is part of the .NET Core SDK. These tools work with .NET Core projects.
When using Visual Studio, consider using the Package Manager Console tools in lieu of the CLI tools. Package Manager Console tools 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 tools
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
To use it as a local tool, restore the dependencies of a project that declares it as a tooling dependency using a tool manifest file.
Run the following commands to verify that EF Core CLI tools are correctly installed:
dotnet ef
The output from the command identifies the version of the tools in use:
_/\__ ---==/ \\ ___ ___ |. \|\ | __|| __| | ) \\\ | _| | _| \_/ | //|\\ |___||_| / \\\/\\ Entity Framework Core .NET Command-line Tools 2.1.3-rtm-32065 <Usage documentation follows, not shown.> <VERSION> to your command. See the Update section of the dotnet tool documentation for more details.
Using the tools
Before using the tools, you might have to create a startup project or set the environment.
Target project and startup project
The commands refer to a project and a startup project.
The project is also known as the target project because it's where the commands add or remove files. By default, the project in the current project in the current directory CLI tools work with .NET Core projects the ASPNETCORE_ENVIRONMENT environment variable before running commands.
Common options
Starting in EF Core 5.0, any additional arguments are passed to the application.
dotnet ef database drop
Drops the database.
Options:
The common options are listed above.
dotnet ef database update
Updates the database to the last migration or to a specified migration.
Arguments:
Options:
The common options are listed above.
The following examples update the database to a specified migration. The first uses the migration name and the second uses the migration ID and a specified connection:
dotnet ef database update InitialCreate dotnet ef database update 20180904195021_InitialCreate --connection your_connection_string
dotnet ef dbcontext info
Gets information about a
DbContext type.
The common options are listed above.
dotnet ef dbcontext list
Lists available
DbContext types.
The common options are listed above.
dotnet ef dbcontext scaffold
Generates code for a
DbContext and entity types for a database. In order for this command to generate an entity type, the database table must have a primary key.
Arguments:
Options:
The common options are listed above.
The following example scaffolds all schemas and tables and puts the new files in the Models folder.
dotnet ef dbcontext scaffold "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models
The following example scaffolds only selected tables and creates the context in a separate folder with a specified name and namespace:
dotnet ef dbcontext scaffold "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models -t Blog -t Post --context-dir Context -c BlogContext --context-namespace New.Namespace
dotnet ef dbcontext script
Generates a SQL script from the DbContext. Bypasses any migrations. Added in EF Core 3.0.
Options:
The common options are listed above.
dotnet ef migrations add
Adds a new migration.
Arguments:
Options:
The common options are listed above.
dotnet ef migrations list
Lists available migrations.
Options:
The common options are listed above.
dotnet ef migrations remove
Removes the last migration (rolls back the code changes that were done for the migration).
Options:
The common options are listed above.
dotnet ef migrations script
Generates a SQL script from migrations.
Arguments:
Options:
The common options are listed above.
The following example creates a script for the InitialCreate migration:
dotnet ef migrations script 0 InitialCreate
The following example creates a script for all migrations after the InitialCreate migration.
dotnet ef migrations script 20180904195021_InitialCreate