为什么运行 "dotnet ef migrations add" 会抛出 Win32Exception 异常?

8

我有一个使用 Entity Framework 的 dotnet core v3.1 项目,其中包含一个 DbContext。 当我尝试运行以下代码时:

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测试时,它返回此警告并且无法开始执行。

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

  1. dotnet
  2. dotnet ef
  3. dotnet ef migrations

但是当我使用这些选项时,会引发异常:

  • dotnet ef migrations add
  • dotnet ef migrations list

另外,我使用详细信息选项运行我的命令,输出如下:

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

以下是在我的环境中运行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

你有其他的项目解决方案吗? - Rahul
这个thread可能会有帮助。 - Yinqiu
我最近看到了这个帖子,但是没有理解它的重点。@Yinqiu - Navid_pdp11
我认为你应该检查你的路径和权限。 - Yinqiu
这是一个普遍的问题,我不能在所有项目上运行此命令。@MehdiKhademloo - Navid_pdp11
显示剩余2条评论
3个回答

7

我尝试删除符号链接,但并没有起作用。

我不得不重新安装dotnet-ef工具:

dotnet new tool-manifest

dotnet tool install dotnet-ef

这解决了问题 :)


6

这个对我有帮助,

我在/usr/local/bin/(macOS)中删除了dotnet symlink


4

我能够通过@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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接