找不到命令 dotnet ef

Command dotnet ef not found

我正在关注 the docs 以创建初始迁移。当我执行 dotnet 时,我得到了帮助部分,这意味着 PATH 工作正常。

然后我尝试从控制台中的文档执行以下命令 window:

dotnet ef migrations add InitialCreate

我收到以下错误:

Could not execute because the specified command or file was not found.
Possible reasons for this include:

我正在谷歌搜索这个问题,但由于版本是新的,所以没有太多可以继续的东西and/or它淹没在早期版本的类似问题中。

我试图强行安装 Microsoft.EntityFrameworkCore 以防需要显式添加。我 运行 进入错误消息,告诉我要选择的最新版本是 2.2.6,降级是不行的。我不确定如何安装与我系统上已有的 SQL 软件包兼容的版本(更不确定这是否是解决此问题的正确方法)。

Detected package downgrade: Microsoft.EntityFrameworkCore from 3.0.0-preview6.19304.10 to 2.2.6. Reference the package directly from the project to select a different version.
Web ->
Microsoft.EntityFrameworkCore.SqlServer 3.0.0-preview6.19304.10 ->
Microsoft.EntityFrameworkCore.Relational 3.0.0-preview6.19304.10 ->
Microsoft.EntityFrameworkCore (>= 3.0.0-preview6.19304.10)
Web -> Microsoft.EntityFrameworkCore (>= 2.2.6)

请参阅 ASP.NET Core 3 Preview 4 的 announcement,其中说明此工具不再 built-in 并且需要显式安装:

The dotnet ef tool is no longer part of the .NET Core SDK

This change allows us to ship dotnet ef as a regular .NET CLI tool that can be installed as either a global or local tool. For example, to be able to manage migrations or scaffold a DbContext, install dotnet ef as a global tool typing the following command:

dotnet tool install --global dotnet-ef

要安装该工具的特定版本(请参阅 nuget.org 中的所有可用版本),请使用以下命令:

dotnet tool install --global dotnet-ef --version 3.1.4

更改的原因在 docs:

中解释

Why

This change allows us to distribute and update dotnet ef as a regular .NET CLI tool on NuGet, consistent with the fact that the EF Core 3.0 is also always distributed as a NuGet package.

此外,您可能需要将以下 NuGet 包添加到您的项目中:

如果您使用 Dockerfile 进行部署,这些是您需要采取的步骤来解决此问题。

更改您的 Dockerfile 以包含以下内容:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
ENV PATH $PATH:/root/.dotnet/tools
RUN dotnet tool install -g dotnet-ef --version 3.1.1

同时将您的 dotnet ef 命令更改为 dotnet-ef

我遇到了同样的问题。我通过卸载我电脑上的所有版本然后重新安装 dotnet.

解决了这个问题

Global tools can be installed in the default directory or in a specific location. The default directories are:

  • Linux/macOS ---> $HOME/.dotnet/tools

  • Windows ---> %USERPROFILE%\.dotnet\tools

If you're trying to run a global tool, check that the PATH environment variable on your machine contains the path where you installed the global tool and that the executable is in that path.

Troubleshoot .NET Core tool usage issues

运行 PowerShell 或命令提示符作为管理员和运行 下面的命令。

dotnet tool install --global dotnet-ef --version 3.1.3

如果您在 Linux 上使用 Snap 软件包 dotnet-sdk,这可以通过更新您的 ~.bashrc 文件等来解决。如下:

#!/bin/bash
export DOTNET_ROOT=/snap/dotnet-sdk/current
export MSBuildSDKsPath=$DOTNET_ROOT/sdk/$(${DOTNET_ROOT}/dotnet --version)/Sdks
export PATH="${PATH}:${DOTNET_ROOT}"
export PATH="$PATH:$HOME/.dotnet/tools"

对于在 MinGW MSYS 上使用 .NET Core CLI 的所有人:

安装后使用

dotnet tool install --global dotnet-ef

将此行添加到 bashrc 文件(C:\msys64\home\username\ - .bashrc(位置取决于您的设置)

export PATH=$PATH:/c/Users/username/.dotnet/tools

我在使用 Ansible 安装 dotnet-ef 工具后遇到了这个问题,在 sudo 上提升了 Ubuntu 的权限。我必须为 Playbook 任务添加 become: no,然后 dotnet-ef 工具对当前用户可用。

  - name: install dotnet tool dotnet-ef
    command: dotnet tool install --global dotnet-ef --version {{dotnetef_version}}
    become: no

我遵循了这些步骤,对我来说效果很好:

  1. 添加来源:

    dotnet nuget add source --name nuget.org https://api.nuget.org/v3/index.json
    
  2. 运行安装命令行:

    dotnet tool install --global dotnet-ef --version 5.0.6
    

有时可能是由于系统内的不同用户造成的。

因此,要解决此问题,您可以在解决方案中本地安装 dotnet-ef,而不是全局添加。

本地安装步骤。

  1. 通过创建本地清单文件 dotnet new tool-manifest

  2. 转到 config 文件夹:

    cd .\.config

  3. 通过以下方式安装工具 dotnet tool install dotnet-ef --version versionNumber。它将成功安装,并且可以在项目中访问其命令。

原因是 - dotnet ef 工具不再是 ASP.NET Core 3.0 中 .NET Core SDK 的一部分。

解决方案:安装 dotnet ef 作为全局工具

步骤:

  1. 运行 PowerShell 或命令提示符作为项目根目录中的管理员
  2. 运行 命令下方。

最新版本:

dotnet tool install --global dotnet-ef

对于特定版本:

dotnet tool install --global dotnet-ef --version <<version_number>>

我通过使用以下命令在本地安装 dotnet-f 工具 解决了这个问题。

如果您正在设置此存储库

dotnet new tool-manifest

dotnet tool install --local dotnet-ef --version 5.0.6

然后使用 dotnet dotnet-ef 而不是 dotnet-ef.

这对我在 Ubuntu

中的 Visual studio 代码有用
dotnet tool install --global dotnet-ef
dotnet tool restore

之后所有的执行都像

一样完成
dotnet tool run dotnet-ef

dotnet dotnet-ef

如果您问自己有哪些版本可用:

此页面上的“版本”选项卡https://www.nuget.org/packages/dotnet-ef/

除此之外,正如其他人已经提到的那样:

dotnet tool install --global dotnet-ef --version 5.0.11

你可以运行这个命令

dotnet tool install dotnet-ef -g

那么一切都会好起来的:)