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

AutoMapper升级至9版本后ResolutionContext无Engine属性的修复求助

AutoMapper 9迁移:ResolutionContext.Engine替代方案

问题核心

AutoMapper 5+版本移除了ResolutionContext.Engine属性,改用ResolutionContext.Mapper(类型为IMapper)实现嵌套映射,同时新版本原生支持更简洁的ResolveUsing重载,无需自定义扩展方法。

修复步骤

  1. 移除自定义扩展方法
    原MappingExtensions.ResolveUsing是为适配旧版本API封装的,AutoMapper 9已原生提供接受Func<TSource, ResolutionContext, TMember>的ResolveUsing重载,可直接删除该扩展类。

  2. 修改映射配置代码
    将原代码中ctx.Engine.Map替换为ctx.Mapper.Map,调整后代码如下:

    .ForMember(a => a.Definition, o =>
    {
        o.Condition(s => s.TypeId == DocumentationType.Medication);
        o.ResolveUsing((d, ctx) => ctx.Mapper.Map<MedicationDefinitionContent>(d.Content.MedicationContentData));
    })
    
  3. 验证映射配置
    确保已提前配置MedicationContentData到MedicationDefinitionContent的映射规则,例如:

    CreateMap<MedicationContentData, MedicationDefinitionContent>();
    

关键说明

  • ResolutionContext.Mapper是AutoMapper 5+新增的属性,完全替代旧版Engine的功能,支持所有映射操作。
  • 新版本的IMemberConfigurationExpression采用三泛型参数(TSource, TDestination, TMember),你已修复该参数错误,无需额外调整。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 16:05:24