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

已配置类但AutoMapper映射失败,求排查解决方案

AutoMapper映射问题排查:NFeMovementInsertPayload → NFeMovement

需要完成NFeMovementInsertPayload到NFeMovement的全类映射(包含所有子类),但已配置映射规则后仍报错,报错指向Fiscal成员。

源类定义

public class NFeMovementInsertPayload : NFeMovementBaseCrudPayload
{
    public NFeMovementInsertPayload()
    {
        Items = new();
        Fiscal = new();
    }

    public List<NFeItemInsertPayload> Items { get; set; }
    public NotaFiscalInsertPayload Fiscal { get; set; }
}

目标类定义

public class NFeMovement
{
    private int id;

    public NFeMovement()
    {
        Fiscal = new NotaFiscal();
        Transport = new MovementTransport();
        Items = new List<NFeMovementItem>(990);
        Issuer = new FiscalMovementIssuer();
        OriginLinkMovement = new MovementLink();
    }

    public decimal? OtherValue { get; set; }
    public decimal NetTotal { get; set; }
    public int RecipientId { get; set; }
    public int PriceListId { get; set; }
    public decimal QuantityItems { get; set; }
    public decimal? ShippingCost { get; set; }
    public FiscalNFeStatus StatusId { get; set; }
    public FiscalMovementType TypeId { get; set; }
    public NotaFiscal Fiscal { get; set; }
    public MovementTransport Transport { get; set; }
    public List<NFeMovementItem> Items { get; set; }
    public FiscalMovementIssuer Issuer { get; set; }
    public MovementLink OriginLinkMovement { get; set; }
}

已配置的映射规则

//payloads
CreateMap<NFeMovementInsertPayload, NFeMovement>()
    .ForMember(
        dest => dest.GrossTotal,
        opt => opt.MapFrom(orig => orig.Items.Sum(x => x.Subtotal)))
    .ForMember(
        dest => dest.QuantityItems,
        opt => opt.MapFrom(orig => orig.Items.Sum(x => x.Quantity)));
CreateMap<NFeItemInsertPayload, NFeMovementItem>();
CreateMap<NotaFiscalInsertPayload, NotaFiscal>();
CreateMap<PagamentoNFeInsertPayload, PagamentoNFe>();

报错信息

Error mapping types.

Mapping types:
NFeMovementInsertPayload -> NFeMovement
LogSistemas.Domynus.Application.Core.Payload.Fiscal.NFeMovementInsertPayload -> LogSistemas.Domynus.Application.Core.Entities.Fiscal.NFeMovement

Type Map configuration:
NFeMovementInsertPayload -> NFeMovement
LogSistemas.Domynus.Application.Core.Payload.Fiscal.NFeMovementInsertPayload -> LogSistemas.Domynus.Application.Core.Entities.Fiscal.NFeMovement

Destination Member:
Fiscal

解决建议

  • 检查命名空间一致性:确认NotaFiscalInsertPayload和NotaFiscal的映射配置中,类的命名空间是否正确,避免因同名类在不同命名空间导致AutoMapper无法匹配到正确的映射规则。
  • 显式指定Fiscal成员映射:即使默认应该自动映射,显式配置可以排除隐式匹配问题,修改主映射规则:
    CreateMap<NFeMovementInsertPayload, NFeMovement>()
        .ForMember(dest => dest.GrossTotal, opt => opt.MapFrom(orig => orig.Items.Sum(x => x.Subtotal)))
        .ForMember(dest => dest.QuantityItems, opt => opt.MapFrom(orig => orig.Items.Sum(x => x.Quantity)))
        // 显式绑定Fiscal的映射逻辑
        .ForMember(dest => dest.Fiscal, opt => opt.MapFrom(orig => orig.Fiscal));
    
  • 验证子映射完整性:检查NotaFiscalInsertPayload到NotaFiscal的映射是否覆盖了所有必要成员,有没有必填字段未配置映射导致失败。可以先简化子映射,只映射几个核心字段测试是否能正常运行。
  • 启用配置校验:在AutoMapper配置完成后调用configuration.AssertConfigurationIsValid(),这会自动检查所有映射的完整性,找出未配置的成员或类型不匹配问题。
  • 排查目标类初始化干扰:目标类NFeMovement构造函数中已初始化Fiscal实例,AutoMapper默认会覆盖该实例,确认是否存在自定义类型转换器、值解析器干扰了Fiscal的映射流程。
  • 检查嵌套映射依赖:如果NotaFiscalInsertPayload内部包含其他嵌套类(如PagamentoNFeInsertPayload),确认这些子映射是否正确配置,没有循环引用或类型不匹配问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 08:07:54