【问题标题】:Can't Add Migration in Visual Studio Code for EntityFrameworkCore无法在 Visual Studio Code 中为 EntityFrameworkCore 添加迁移
【发布时间】:2021-08-29 00:56:41
【问题描述】:

我正在尝试使用 EntityFrameworkCore 在 Visual Studio Code 中构建 DotNetCore WebApi。

我已经阅读了许多教程,但我总是卡在它说的地方:

运行Add-Migration InitialCreate

我收到以下错误:

“add-migration”一词未被识别为 cmdlet、函数、脚本文件或可运行程序的名称

其他教程说我应该运行这个命令 dotnet ef migrations add InitialCreate

这给了我错误: No executable found matching command "dotnet-ef"

这些是我的 csproj 中的引用:

<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2"/>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3"/>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2"/>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="1.1.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.1.0-preview4-final"/>

如何添加迁移?

【问题讨论】:

  • 你是在proj文件所在的目录下运行命令吗?
  • 没有dotnet-efdotnet ef 另外,确保您从包含project.json 的目录运行dotnet ef
  • @tchelidze dotnet ef 会调用dotnet-ef,这很正常。
  • Last comment 可能有用
  • 您的项目文件是否包含Microsoft.EntityFrameworkCore.Tools.DotnetDotNetCliToolReference 条目?

标签: asp.net-core visual-studio-code entity-framework-core


【解决方案1】:

为了能够使用命令行 CLI 来管理迁移,您需要将这些工具作为项目的一部分。确保你的项目中有这样的东西:

<ItemGroup>
    <DotNetCliToolReference 
        Include="Microsoft.EntityFrameworkCore.Tools.Dotnet" 
        Version="1.0.1" />
</ItemGroup>

接下来从命令行,确保您与 csproj 文件位于同一目录中并运行以下命令:

dotnet ef migrations add InitialCreate

【讨论】:

    【解决方案2】:

    要使“dotnet-ef”命令在 Visual Studio Code 中可用,请通过运行上述命令安装 dotnet ef 工具。

    dotnet tool install --global dotnet-ef
    

    添加迁移:

    dotnet ef migrations add MigrationName
    

    尝试执行

    dotnet ef database update
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-04
      • 2017-12-06
      • 2015-10-17
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      • 1970-01-01
      相关资源
      最近更新 更多