EF Core - Sử dụng lệnh CLI

Sử dụng lệnh Command Line Interface cho Migrations

Sử dụng .NET Core Command List Interface để thực thi các lệnh của EF Core. Để sử dụng .NET CLI, hãy thêm <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" /> under <ItemGroup> chú ý chỉnh sửa tập tin  .NET Core project's .csproj.

Mở dấu nhắc lệnh và chuyển đến thư mục gốc của dự án của bạn và nhập dotnet ef --help để liệt kê các lệnh của EF Core, như được hiển thị bên dưới.

C:> dotnet ef --help
Entity Framework Core .NET Command Line Tools 2.0.0-rtm-26452

Usage: dotnet ef [options] [command]

Options:
  --version        Show version information
  -h|--help        Show help information
  -v|--verbose     Show verbose output.
  --no-color       Don't colorize output.
  --prefix-output  Prefix output with level.

Commands:
  database    Commands to manage the database.
  dbcontext   Commands to manage DbContext types.
  migrations  Commands to manage migrations.

Use "dotnet ef [command] --help" for more information about a command.
    

As you can see above, there are three main EF commands available: database, dbcontext and migrations. The following table lists all EF commands and sub commands.

Command Sub Commands Usage
Database drop Drops the database.
update Updates the database to a specified migration.
DbContext info Gets information about a DbContext type.
list Lists available DbContext types.
scaffold Scaffolds a DbContext and entity types for a database.
Migration add Adds a new migration.
list Lists available migrations.
remove Removes the last migration.
script: Generates a SQL script from migrations.

Let's see available options for each command.

Database Drop

> dotnet ef database drop

Usage: dotnet ef database drop [options]

Options:
  -f|--force                             Don't confirm.
  --dry-run                              Show which database would be dropped, but don't drop it.
  -c|--context <DBCONTEXT>               The DbContext to use.
  -p|--project <PROJECT>                 The project to use.
  -s|--startup-project <PROJECT>         The startup project to use.
  --framework <FRAMEWORK>                The target framework.
  --configuration <CONFIGURATION>        The configuration to use.
  --runtime <RUNTIME_IDENTIFIER>         The runtime to use.
  --msbuildprojectextensionspath <PATH>  The MSBuild project extensions path. Defaults to "obj".
  --no-build                             Don't build the project. Only use this when 
                                         the build is up-to-date.

Database Update

> dotnet ef database update

Usage: dotnet ef database update [arguments] [options]

Arguments:
  <MIGRATION>  The target migration. If '0', all migrations will be reverted. De
faults to the last migration.

Options:
  -c|--context <DBCONTEXT>               The DbContext to use.
  -p|--project <PROJECT>                 The project to use.
  -s|--startup-project <PROJECT>         The startup project to use.
  --framework <FRAMEWORK>                The target framework.
  --configuration <CONFIGURATION>        The configuration to use.
  --runtime <RUNTIME_IDENTIFIER>         The runtime to use.
  --msbuildprojectextensionspath <PATH>  The MSBuild project extensions path. Defaults to "obj".
  --no-build                             Don't build the project. Only use this when
                                         the build is up-to-date.

DbContext Info

> dotnet ef dbcontext info

Usage: dotnet ef dbcontext info [options]

Options:
  --json                                 Show JSON output.
  -c|--context <DBCONTEXT>               The DbContext to use.
  -p|--project <PROJECT>                 The project to use.
  -s|--startup-project <PROJECT>         The startup project to use.
  --framework <FRAMEWORK>                The target framework.
  --configuration <CONFIGURATION>        The configuration to use.
  --runtime <RUNTIME_IDENTIFIER>         The runtime to use.
  --msbuildprojectextensionspath <PATH>  The MSBuild project extensions path. Defaults to "obj".
  --no-build                             Don't build the project. Only use this 
                                         when the build is up-to-date.