Dot-Net-Core

為什麼“dotnet ef 遷移添加”會引發 Win32Exception?

  • February 21, 2022

我有一個帶有實體框架 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 基本作業系統(Ubuntu 18.04.4 LTS)和 Rider IDE。

這可能無關緊要,但是當我嘗試調試我的xunit測試時,它會返回此警告並且無法開始執行。

所有這些命令都能正常工作並顯示所有選項:

  1. 點網
  2. dotnet ef
  3. dotnet ef 遷移

但是當我使用這些選項時引發異常:

  • dotnet ef 遷移添加
  • dotnet ef 遷移列表

另外,我使用詳細選項執行命令,這是輸出:

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

對我有幫助,

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

刪除符號連結對我不起作用。我不得不重新安裝 dotnet-ef 工具:

dotnet new tool-manifest

dotnet tool install dotnet-ef

這解決了問題:)

引用自:https://stackoverflow.com/questions/66022881