首页 > 解决方案 > “Microsoft.EntityFrameworkCore.Query.QueryableMethods”的类型初始化程序引发异常

问题描述

我有一个功能应用程序,当我使用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 启动函数应用程序时,它工作正常。

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

有任何想法吗?

谢谢

函数应用程序.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>

视觉工作室版本

Microsoft Visual Studio Professional 2019
Version 16.10.4

标签: c#entity-framework-coreazure-functions

解决方案


当我升级Microsoft.EntityFrameworkCore到依赖时问题就消失5.0.0了。SecondLib

<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>

推荐阅读