'Microsoft.EntityFrameworkCore.Query.QueryableMethods' 的类型初始值设定项抛出异常

The type initializer for 'Microsoft.EntityFrameworkCore.Query.QueryableMethods' threw an exception

我有一个函数应用程序,当我使用 azure-functions-core-tools@4.0.3780 start 命令 运行 时它会抛出以下错误。

func start
System.Private.CoreLib: Exception while executing function: Test. 
Microsoft.EntityFrameworkCore: The type initializer for 
'Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor' 
threw an exception. Microsoft.EntityFrameworkCore: 
The type initializer for 
'Microsoft.EntityFrameworkCore.Query.QueryableMethods' threw an exception. 
System.Linq: Sequence contains more than one matching element.

入口点

private readonly IRepository _repository;

[FunctionName("Test")]
        public async Task TestAsync(
            [ServiceBusTrigger(
                "%topic%",
                "%subscription%",
                Connection = "connectionString")]
            Message message)
    {
        var result = await _repository.ToListAsync();
    }

从 Visual Studio 启动函数应用程序时工作正常。

我想我可以通过在函数应用程序 csproj 中直接引用 Microsoft.EntityFrameworkCore 来摆脱它。

有什么想法吗?

谢谢

FunctionApp.csproj

  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" 
    Version="4.3.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.13" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="3.1.11" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\FirstLib\FirstLib.csproj" />
  </ItemGroup>

FirstLib.csproj

  <ItemGroup>
    <ProjectReference Include="..\SecondLib\SecondLib.csproj" />
  </ItemGroup>

SecondLib.csproj

 <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite" Version="3.1.8" />
  </ItemGroup>

Visual Studio版本

Microsoft Visual Studio Professional 2019
Version 16.10.4

QueryableMethods Class 兼容 Microsoft.EntityFrameworkCore v5.0.0

尝试通过将目标框架更改为 net5.0 来删除对 EntityFramework 的引用。

当我在 SecondLib 依赖项中将 Microsoft.EntityFrameworkCore 升级到 5.0.0 时,问题消失了。

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite" Version="5.0.0" />
  </ItemGroup>

TargetFramework 没有被触及

<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>