Linux 上的 C# DotNet Entity Framework Core Migrations 错误

Posted

技术标签:

【中文标题】Linux 上的 C# DotNet Entity Framework Core Migrations 错误【英文标题】:C# DotNet Entity Framework Core Migrations Error on Linux 【发布时间】:2021-06-14 02:04:46 【问题描述】:

我在 Linux 上使用 JetBrains Rider。每当我运行dotnet ef migrations add InitialCreate 时,它都会在终端中执行此操作并给我这个错误:

Build started...
Build succeeded.
Xlib:  extension "NV-GLX" missing on display ":0.0".
Xlib:  extension "NV-GLX" missing on display ":0.0".
0104:fixme:ntdll:NtQuerySystemInformation info_class SYSTEM_PERFORMANCE_INFORMATION
System.PlatformNotSupportedException: Cannot find 'Mono.SystemDependencyProvider, System' dependency
  at Mono.DependencyInjector.get_SystemProvider () [0x0003d] in <a9da539eb2f94b1a84ab84db52450d46>:0
  at System.Security.Cryptography.X509Certificates.X509Helper.get_CertificateProvider () [0x00000] in <a9da539eb2f94b1a84ab84db52450d46>:0
  at System.Security.Cryptography.X509Certificates.X509Helper.Import (System.Byte[] rawData, Microsoft.Win32.SafeHandles.SafePasswordHandle password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags)
 [0x00000] in <a9da539eb2f94b1a84ab84db52450d46>:0
  at System.Security.Cryptography.X509Certificates.X509Certificate..ctor (System.String fileName, System.String password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags) [0x0003e] in <a9da539eb2f94
b1a84ab84db52450d46>:0
  at System.Security.Cryptography.X509Certificates.X509Certificate..ctor (System.String fileName) [0x00000] in <a9da539eb2f94b1a84ab84db52450d46>:0
  at System.Security.Cryptography.X509Certificates.X509Certificate.CreateFromSignedFile (System.String filename) [0x00000] in <a9da539eb2f94b1a84ab84db52450d46>:0
  at System.Security.Policy.Evidence.GetDefaultHostEvidence (System.Reflection.Assembly a) [0x00097] in <a9da539eb2f94b1a84ab84db52450d46>:0
  at System.AppDomain.get_Evidence () [0x00045] in <a9da539eb2f94b1a84ab84db52450d46>:0
  at (wrapper remoting-invoke-with-check) System.AppDomain.get_Evidence()
  at System.AppDomain.CreateDomain (System.String friendlyName, System.Security.Policy.Evidence securityInfo, System.AppDomainSetup info) [0x000de] in <a9da539eb2f94b1a84ab84db52450d46>:0
  at Microsoft.EntityFrameworkCore.Tools.AppDomainOperationExecutor..ctor (System.String assembly, System.String startupAssembly, System.String projectDir, System.String dataDirectory, System.String rootNamespace, System.String lang
uage, System.String[] remainingArguments) [0x0004c] in <27660142d7fa4f02b91c8d5e3bfbdb6d>:0
  at Microsoft.EntityFrameworkCore.Tools.Commands.ProjectCommandBase.CreateExecutor (System.String[] remainingArguments) [0x00042] in <27660142d7fa4f02b91c8d5e3bfbdb6d>:0
  at Microsoft.EntityFrameworkCore.Tools.Commands.MigrationsAddCommand.Execute (System.String[] args) [0x00000] in <27660142d7fa4f02b91c8d5e3bfbdb6d>:0
  at Microsoft.EntityFrameworkCore.Tools.Commands.CommandBase+<>c__DisplayClass0_0.<Configure>b__0 (System.String[] args) [0x0003b] in <27660142d7fa4f02b91c8d5e3bfbdb6d>:0
  at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute (System.String[] args) [0x000df] in <27660142d7fa4f02b91c8d5e3bfbdb6d>:0
  at Microsoft.EntityFrameworkCore.Tools.Program.Main (System.String[] args) [0x0002e] in <27660142d7fa4f02b91c8d5e3bfbdb6d>:0
Cannot find 'Mono.SystemDependencyProvider, System' dependency

如果有人知道答案,谢谢!

【问题讨论】:

【参考方案1】:

我在 .net 5 web API 项目中遇到了同样的问题

EF 工具环顾 CreateWebHostBuilder 方法,找不到时返回错误。

directories:

/src 
    |-- /YourPorject.Infrastructure
    |-- /YourPorject.API

public static IHostBuilder CreateWebHostBuilder(string[] args)

    return Host.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((context, config) =>
            
                config.AddJsonFile(
                    context.HostingEnvironment.ContentRootPath + $"/Config/appsettings-dev.json",
                    false, false);
            )
            .ConfigureWebHostDefaults(webBuilder =>  webBuilder.UseStartup<Startup>(); );

使用 bash 创建和更新数据库

    cd src/
    dotnet ef migrations add Init 
    --project YourPorject.Infrastructure/YourPorject.Infrastructure.csproj  
    --startup-project YourPorject.API/YourPorject.API.csproj 
    -o ./Repositories/Migrations/

    cd src/
    dotnet ef database update 
    --project YourPorject.Infrastructure/YourPorject.Infrastructure.csproj 
    --startup-project YourPorject.API/YourPorject.API.csproj

使用这个包(很重要

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.8" />
</ItemGroup>

【讨论】:

以上是关于Linux 上的 C# DotNet Entity Framework Core Migrations 错误的主要内容,如果未能解决你的问题,请参考以下文章

centos linux上的dotnet restore错误:访问路径被拒绝

如何在 Entity Framework Core cli 工具中使用来自 dotnet 6 最小 API 的配置

Mac 上的“dotnet publish”创建一个 .dll,但在 Linux 上生成一个可执行文件?

如何在dotnet core Entity Framework中对多个表的数据进行GroupBy操作?

dotnet跨平台微软昨天宣布正式发布.NET Core RC2和.NET Core SDK Preview 1,还有Entity Framework Core RC2

如何将 C# 中的方法指针传递给 C 库?