Automapper。尝试映射嵌套类型时出错

问题描述 投票:0回答:1

我正在尝试将一个对象映射到另一个对象:

Mapper.CreateMap<ShippingAddressModel, ShippingAddress>()
                    .ForMember(x => x.Addresses.Name, opts => opts.MapFrom(x => x.Name));

但是我有一个错误:

表达式'x => x.Addresses.Name'必须解析为顶级成员而不是任何子对象的属性。请在子类型或AfterMap选项上使用自定义解析程序。

c# .net automapper
1个回答
0
投票

这应该工作:


Mapper.CreateMap<ShippingAddressModel, ShippingAddress>()
    .ForMember(x => x.Addresses, opt => opt.MapFrom(model => model));
Mapper.CreateMap<ShippingAddressModel, Addresses>()
    .ForMember(x => x.Name, opt => opt.MapFrom(model => model.Name));

© www.soinside.com 2019 - 2024. All rights reserved.