站点图标 神之眼的博客

执行命令dotnet ef migrations add报错, Could not execute because the specified command or file was not found. —解决方案

當前文章的短網址連結為: https://unos.top/9r7o

环境:

MacOS, Rider 2024, NET 8.0.4

JetBrains Rider中执行数据库迁移命令,例如

/usr/local/share/dotnet/dotnet ef migrations add ...

结果,报错如下

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 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.

上述命令是通过Rider执行的Migration, 因此第1条不太可能有问题

而第2和第3都说提示说,dotnet-ef环境变量有问题, 因此,尝试再次安装dotnet-ef, 看系统如何提示

dotnet tool install -g dotnet-ef

执行上述命令后,终端提示如下

% dotnet tool install -g dotnet-ef
Tools directory '/Users/marcus/.dotnet/tools' is not currently on the PATH environment variable.
If you are using zsh, you can add it to your profile by running the following command:

cat << \EOF >> ~/.zprofile
# Add .NET Core SDK tools
export PATH="$PATH:/Users/marcus/.dotnet/tools"
EOF

And run `zsh -l` to make it available for current session.

You can only add it to the current session by running the following command:

export PATH="$PATH:/Users/marcus/.dotnet/tools"

Tool 'dotnet-ef' was reinstalled with the stable version (version '8.0.4').

由此可见,系统中全局PATH环境变量中并没有找到dotnet-ef, 才会出现上述错误, 因此在终端执行以下命令即可解决

cat << \EOF >> ~/.zprofile
# Add .NET Core SDK tools
export PATH="$PATH:/Users/marcus/.dotnet/tools"
EOF

如果只是想临时使用dotnet tools, 在终端执行以下命令即可

export PATH="$PATH:/Users/marcus/.dotnet/tools"

最后,让全局PATH配置文件立即生效

source ~/.zprofile

再次查看PATH环境是否正确

echo $PATH

退出移动版