找不到命令“ dotnet ef”?

Was*_*ama 25 c# entity-framework csproj .net-core asp.net-core

我在Arch VScode上使用.NET Core 2.0,并尝试使EF工具正常工作,但我不断收到该错误“找不到命令dotnet ef”。我只是到处看,所有建议都没有。因此,如果可以的话,请您提供帮助。

运行“ dotnet ef”的结果

[wasiim@wasiim-PC WebApiServerApp]$ dotnet ef --help
Cannot find command 'dotnet ef', please run the following command to install

dotnet tool install --global dotnet-ef
[wasiim@wasiim-PC WebApiServerApp]$ dotnet tool list -g
Package Id            Version      Commands        
---------------------------------------------------
dotnet-dev-certs      2.2.0        dotnet-dev-certs
dotnet-ef             2.2.3        dotnet-ef       
[wasiim@wasiim-PC WebApiServerApp]$ 
Run Code Online (Sandbox Code Playgroud)

这是dotnet --info结果,如果有帮助的话

[wasiim@wasiim-PC WebApiServerApp]$ dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   2.2.105
 Commit:    7cecb35b92

Runtime Environment:
 OS Name:     arch
 OS Version:  
 OS Platform: Linux
 RID:         arch-x64
 Base Path:   /opt/dotnet/sdk/2.2.105/

Host (useful for support):
  Version: 2.2.3
  Commit:  6b8ad509b6

.NET Core SDKs installed:
  2.2.105 [/opt/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.NETCore.App 2.2.3 [/opt/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download
Run Code Online (Sandbox Code Playgroud)

这是我的.csproj文件

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Lucene.Net.Analysis.Common" Version="4.8.0-beta00005" />
    <PackageReference Include="Lucene.Net.QueryParser" Version="4.8.0-beta00005" />
    <PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.5" />
   <PackageReference Include="Lucene.Net" Version="4.8.0-beta00005" />
    <PackageGroup Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.4" />
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />

  </ItemGroup>

</Project>
Run Code Online (Sandbox Code Playgroud)

Gar*_*rth 76

In my case, the tools folder didn't exist inside %USERPROFILE%\.dotnet\ so I had to run the command dotnet tool install --global dotnet-ef to install dotnet ef. Then I was able to run dotnet ef...

This was the result of the above install command:

这是上述安装命令的结果

  • 这应该是答案 (7认同)
  • 不应该是问题中已经存在的答案。答案是安装后更新`$PATH` (2认同)

Oma*_*jid 16

如何解决这个问题

对于LinuxmacOS,在shell的配置中添加一行:

当您启动新的外壳程序/终端时(或下次登录时),dotnet ef应该可以使用。

对于Windows

有关如何添加到环境变量的信息,请参见此问题PATH

您需要添加%USERPROFILE%\.dotnet\tools到中PATH

这是怎么回事?

此故障的.NET Core 3.0(预览版)更能说明问题:

$ dotnet ef
Could not execute because the specified command or file was not found.
Possible reasons for this include:
  * You misspelled a built-in dotnet command.
  * You intended to execute a .NET Core program, but dotnet-ef does not exist.
  * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
Run Code Online (Sandbox Code Playgroud)

第二个和第三个都涉及dotnet尝试查找dotnet-ef命令,但是找不到它。如第三点所述,dotnet-ef不在您的道路上。

是文档所说的

可以将Global Tools安装在默认目录或特定位置。默认目录为:

OS Path

Linux/macOS $HOME/.dotnet/tools

Windows %USERPROFILE%\.dotnet\tools

因此,您应该添加$HOME/.dotnet/tools/到中$PATH

但也请注意文档中的这一部分:

这些位置会在首次运行SDK时添加到用户的路径中,因此可以直接调用在那里安装的Global Tools。

因此,听起来好像出了点问题。如果使用手动tarball安装,则SDK可能已损坏,应将此错误报告给Microsoft。如果您使用分发包,他们会搞砸,您应该将此作为错误报告给他们。

  • 这不是正确的答案。正确的答案是在命令提示符中运行“dotnet tool install -g dotnet-ef”。只有之后您才应该参考上面的答案来正确设置 PATH 变量。 (6认同)
  • @JohanWintgens 我明白你在说什么,但我认为这是所问问题的答案。该问题已经包含运行“dotnet tool install...”的步骤。如果问题不包括这一点,那么你是完全正确的。 (3认同)

Eln*_*oor 14

对于在更新 Visual Studio 或 .NET Core 包后遇到问题的人,这是由于 .NET Core 3 中的更新,通过dotnet ef从 .NET Core 中删除并使其成为一个单独的包,可以通过以下方式安装:

dotnet tool install -g dotnet-ef

作为参考,请参阅此答案和此重大更改


Ali*_*Ali 8

我已经尝试了所有以前的答案,但它在我的 Mac 上无法使用最新的macOS v10.15 (Catalina) 更新。

例如,如果您使用 .NET Core 3 版本,则需要运行以下命令:

export PATH="$PATH:$HOME/.dotnet/tools/"
export DOTNET_SKIP_FIRST_TIME_EXPERIENCE=false
export DOTNET_ADD_GLOBAL_TOOLS_TO_PATH=true

dotnet tool install --global dotnet-ef --version 3.0.0
Run Code Online (Sandbox Code Playgroud)


Dar*_*ave 5

dotnet ef database update
Run Code Online (Sandbox Code Playgroud)

如果上面的命令给你一个错误,请按照以下步骤操作。

  1. 检查路径 %USERPROFILE%.dotnet\ 是否存在

  2. 如果没有,则运行以下命令

    dotnet tool install -g dotnet-ef
    
    Run Code Online (Sandbox Code Playgroud)
  3. 现在再次检查路径并为以下路径设置环境变量

    %USERPROFILE%\.dotnet\tool
    
    Run Code Online (Sandbox Code Playgroud)
  4. 现在在 cmd 中,设置数据库上下文文件所在的路径,然后运行以下命令

    dotnet ef database update
    
    Run Code Online (Sandbox Code Playgroud)

注意:它适用于 .NET Core 3.0。


Ran*_*vey 5

关于路径修复:

请注意,这会将路径添加到用户路径,而不是系统路径环境变量。从 Visual Studio 启动“开发人员命令提示符”或“开发人员 PowerShell”时,它不使用用户路径变量。您还需要将其添加到系统环境变量中。

此外,您需要重新启动 Visual Studio Code 才能使更改生效。