为什么 "dotnet ef migrations add" 抛出 Win32Exception?

Why does "dotnet ef migrations add" throw a Win32Exception?

我有一个带有 entity framework DbContext 的 dotnet core v3.1 项目。 当我尝试 运行:

dotnet ef migrations add someMigrationName

将抛出此异常:

System.ComponentModel.Win32Exception (2): No such file or directory
   at System.Diagnostics.Process.ForkAndExecProcess(String filename, String[] argv, String[] envp, String cwd, Boolean redirectStdin, Boolean redirectStdout, Boolean redirectStderr, Boolean setCredentials, UInt32 userId, UInt32 groupId, UInt32[] groups, Int32& stdinFd, Int32& stdoutFd, Int32& stderrFd, Boolean usesTerminal, Boolean throwOnNoExec)
   at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at Microsoft.EntityFrameworkCore.Tools.Exe.Run(String executable, IReadOnlyList`1 args, String workingDirectory, Boolean interceptOutput)
   at Microsoft.EntityFrameworkCore.Tools.Project.FromFile(String file, String buildExtensionsDir, String framework, String configuration, String runtime)
   at Microsoft.EntityFrameworkCore.Tools.RootCommand.Execute(String[] _)
   at Microsoft.EntityFrameworkCore.Tools.Commands.CommandBase.<>c__DisplayClass0_0.<Configure>b__0(String[] args)
   at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args)
   at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)
No such file or directory

我的开发环境是 Linux elementary os (Ubuntu 18.04.4 LTS) 和 rider IDE.

它可能无关紧要,但是当我尝试调试我的 xunit 测试它 returns 这个警告并且无法开始执行。

所有这些命令都能正常工作并显示所有选项:

  1. 网络
  2. dotnet ef
  3. dotnet ef 迁移

但是当我使用这些选项时出现异常:

此外,我 运行 我的命令带有 verbose 选项,这是输出:

Using project '/media/navid/9bf0167c-d789-4e06-9e23-4023ec551745/Stuff/Work/SabaCell/venus/Venus.CustomerProfileManagement/Venus.CustomerProfileManagement.csproj'.
Using startup project '/media/navid/9bf0167c-d789-4e06-9e23-4023ec551745/Stuff/Work/SabaCell/venus/Venus.CustomerProfileManagement/Venus.CustomerProfileManagement.csproj'.
Writing '/media/navid/9bf0167c-d789-4e06-9e23-4023ec551745/Stuff/Work/SabaCell/venus/Venus.CustomerProfileManagement/obj/Venus.CustomerProfileManagement.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=/tmp/tmpUud0Xf.tmp /verbosity:quiet /nologo /media/navid/9bf0167c-d789-4e06-9e23-4023ec551745/Stuff/Work/SabaCell/venus/Venus.CustomerProfileManagement/Venus.CustomerProfileManagement.csproj
System.ComponentModel.Win32Exception (2): No such file or directory
   at System.Diagnostics.Process.ForkAndExecProcess(String filename, String[] argv, String[] envp, String cwd, Boolean redirectStdin, Boolean redirectStdout, Boolean redirectStderr, Boolean setCredentials, UInt32 userId, UInt32 groupId, UInt32[] groups, Int32& stdinFd, Int32& stdoutFd, Int32& stderrFd, Boolean usesTerminal, Boolean throwOnNoExec)
   at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at Microsoft.EntityFrameworkCore.Tools.Exe.Run(String executable, IReadOnlyList`1 args, String workingDirectory, Boolean interceptOutput)
   at Microsoft.EntityFrameworkCore.Tools.Project.FromFile(String file, String buildExtensionsDir, String framework, String configuration, String runtime)
   at Microsoft.EntityFrameworkCore.Tools.RootCommand.Execute(String[] _)
   at Microsoft.EntityFrameworkCore.Tools.Commands.CommandBase.<>c__DisplayClass0_0.<Configure>b__0(String[] args)
   at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args)
   at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)
No such file or directory

这是在我的环境中 运行ning dotnet --info 的结果:

.NET Core SDK (reflecting any global.json):
 Version:   3.1.405
 Commit:    3fae16e62e

Runtime Environment:
 OS Name:     elementary
 OS Version:  5.1.7
 OS Platform: Linux
 RID:         linux-x64
 Base Path:   /usr/share/dotnet/sdk/3.1.405/

Host (useful for support):
  Version: 3.1.11
  Commit:  f5eceb8105

.NET Core SDKs installed:
  3.1.405 [/usr/share/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.App 3.1.11 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.11 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

This帮了我,

我在 /usr/local/bin/ (macOS)

中删除了 dotnet symlink

我能够通过@bjorn 的回答解决这个问题。当我尝试使用 snap 安装 .net sdk 时,我的 /usr/local/bin 中有一个指向 /snap/... 的符号链接,但没有成功,因此我尝试了另一种方法。只是补充@bjorn 的回答,这就是我所做的:

检查您是否有符号链接:

ls -l /usr/local/bin/

它将看起来像这样:

lrwxrwxrwx 1 root root       54 jul 22 09:46 dotnet -> /snap/...

然后删除它:

sudo rm /usr/local/bin/dotnet

删除符号链接对我不起作用。 我不得不重新安装 dotnet-ef 工具:

dotnet new tool-manifest

dotnet tool install dotnet-ef

解决了问题 :)