You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用HotChocolate搭建GraphQL Server时遇get_AdHocMapper未实现错误

解决HotChocolate GraphQL Server中EF Core的"get_AdHocMapper未实现"错误

错误详情

执行GraphQL查询时返回如下错误:

{
  "errors": [
    {
      "message": "Unexpected Execution Error",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "managers"
      ],
      "extensions": {
        "message": "Method 'get_AdHocMapper' in type 'Microsoft.EntityFrameworkCore.Storage.Internal.RelationalDatabaseFacadeDependencies' from assembly 'Microsoft.EntityFrameworkCore.Relational, Version=7.0.9.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation.",
        "stackTrace": "   at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkRelationalServicesBuilder.TryAddCoreServices()\n   at Microsoft.Extensions.DependencyInjection.NpgsqlServiceCollectionExtensions.AddEntityFrameworkNpgsql(IServiceCollection serviceCollection)\n   at Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.Internal.NpgsqlOptionsExtension.ApplyServices(IServiceCollection services)\n   at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.ApplyServices(IDbContextOptions options, ServiceCollection services)\n   at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<GetOrAdd>g__BuildServiceProvider|4_1(IDbContextOptions _, ValueTuple`2 arguments)\n   at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c.<GetOrAdd>b__4_0(IDbContextOptions contextOptions, ValueTuple`2 tuples)\n   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd[TArg](TKey key, Func`3 valueFactory, TArg factoryArgument)\n   at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.GetOrAdd(IDbContextOptions options, Boolean providerRequired)\n   at Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options)\n   at AssistantContext..ctor(DbContextOptions`1 options) in /Users/mahmoud/testapi/db/dbcontext.cs:line 13\n   at lambda_method48(Closure, IServiceProvider, DbContextOptions`1)\n   at Microsoft.EntityFrameworkCore.Internal.DbContextFactory`1.CreateDbContext()\n   at Microsoft.EntityFrameworkCore.Internal.DbContextFactory`1.CreateDbContextAsync(CancellationToken cancellationToken)\n   at HotChocolate.Types.EntityFrameworkObjectFieldDescriptorExtensions.<>c__DisplayClass2_1`1.<<UseDbContext>b__4>d.MoveNext()\n--- End of stack trace from previous location ---\n   at HotChocolate.Execution.Processing.Tasks.ResolverTask.ExecuteResolverPipelineAsync(CancellationToken cancellationToken)\n   at HotChocolate.Execution.Processing.Tasks.ResolverTask.TryExecuteAsync(CancellationToken cancellationToken)"
      }
    }
  ]
}

环境信息

  • MacOS + VSCode + .NET 8.0
  • 数据库:PostgreSQL

使用的NuGet包

<ItemGroup>
    <PackageReference Include="HotChocolate.AspNetCore" Version="13.4.0" />
    <PackageReference Include="HotChocolate.Data.EntityFramework" Version="13.4.0" />
    <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0-preview.4.23260.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.9">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
  </ItemGroup>

服务配置代码

builder.Services.AddDbContextFactory<AssistantContext>( o => o.UseNpgsql("Host=127.0.0.1;Database=managingassistant;Username=root;Password=root"));
builder.Services
     .AddGraphQLServer()
     .AddQueryType<Query>()
     .RegisterDbContext<AssistantContext>(DbContextKind.Synchronized)

Query类代码

public class Query
{
    [UseDbContext(typeof(AssistantContext))]
    public IQueryable<Manager> GetManagers([Service(ServiceKind.Synchronized)] AssistantContext db)
    {
        return db.Managers;
    }
}

解决方案

核心问题

错误根源是EF Core相关包版本与.NET 8不兼容:使用了.NET 8 runtime,但EF Core和Npgsql.EntityFrameworkCore.PostgreSQL都是7.x版本,同时引用了预览版的Microsoft.AspNetCore.OpenApi,版本不匹配导致类型成员缺失。

修复步骤

  1. 统一EF Core相关包版本到.NET 8稳定版
    更新NuGet包引用,将所有EF Core相关包升级到8.0.0(与.NET 8 runtime匹配):

    <ItemGroup>
      <PackageReference Include="HotChocolate.AspNetCore" Version="13.4.0" />
      <PackageReference Include="HotChocolate.Data.EntityFramework" Version="13.4.0" />
      <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0" />
      <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0">
        <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        <PrivateAssets>all</PrivateAssets>
      </PackageReference>
      <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
      <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
    </ItemGroup>
    
  2. 验证服务注册与Resolver逻辑

    • 使用AddDbContextFactory注册上下文,搭配RegisterDbContext<AssistantContext>(DbContextKind.Synchronized)是正确的
    • Query类中的[UseDbContext]和[Service(ServiceKind.Synchronized)]注解使用无误,无需修改
  3. 清理并重建项目

    • 执行dotnet clean清理旧的编译文件
    • 执行dotnet restore重新安装匹配版本的NuGet包
    • 重新编译并运行项目

内容的提问来源于stack exchange,提问作者Mahmoud Moravej

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 20:35:14