ef core 更新数据库命令

在 Entity Framework Core 中,更新数据库的常用命令如下:

1. 基本命令(通过 .NET CLI):

bash
dotnet ef database update

2. 指定迁移版本:

bash
dotnet ef database update MigrationName

(将 MigrationName 替换为特定的迁移名称)

3. 从空白项目开始:

如果是首次运行,需要先创建迁移再更新:

bash
dotnet ef migrations add InitialCreate
dotnet ef database update

4. 通过 PMC (Package Manager Console):

如果你使用 Visual Studio 的包管理器控制台:

powershell
Update-Database

或指定迁移:

powershell
Update-Database -Migration MigrationName

常用参数:

  • --context <DbContext>:指定要使用的 DbContext 类型
  • --connection <ConnectionString>:指定连接字符串
  • --environment <Environment>:指定环境(如 Development/Production)

注意事项:

  1. 确保已安装 EF Core 工具:
    bash
    dotnet tool install --global dotnet-ef
  2. 项目需包含 Microsoft.EntityFrameworkCore.Design 包
  3. 命令需在项目目录(包含 DbContext 的项目)中执行

 

posted @ 2025-05-14 21:46  阿维net  阅读(172)  评论(0)    收藏  举报