具有嵌套实体和不同表的 AutoMapper

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

在上述情况下,我收到了一个包含大量属性和两个嵌套 DTO 的 JSON 对象 (PreSalesDTO),我需要将它们映射到实际模型并将它们存储在不同的表中。我正在使用 .NET 7 和 AutoMapper 来尝试映射这些类,但我在嵌套类方面遇到了很多困难。

典型

CreateMap<PreSalesDTO, PreSales>()

并没有真正起作用,即使使用了 ForMember 选项

我不太确定如何从这里开始。 AutoMapper 不是我非常熟悉的东西。

感谢您的帮助。

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

你的问题有点奇怪,但我已经尝试实现了;将以下行添加到您的

MappingProfile

        CreateMap<PreSalesDto, PreSales>();
        CreateMap<CustomerDto, Customer>();
        CreateMap<PreSalesProductDTO, PreSalesProduct>();

        CreateMap<PreSalesDto, ( PreSales,PreSalesProduct, Customer)>()
            .ForMember(x=>x.Item1,config=>config.MapFrom(src=>new PreSalesDto() {a = src.a,b=src.b,c=src.c}))
            .ForMember(x=>x.Item2,config=>config.MapFrom(src=>src.PreSalesProductDto))
            .ForMember(x => x.Item3, config => config.MapFrom(src => src.CustomerDto));

而当我想映射时写了下面的代码:

var mapped= _mapper.Map<( PreSales,PreSalesProduct, Customer)>(preSalesDto);

mapped
对象是一个元组,
Item1
Item2
Item3
分别是
PreSales
PreSalesProduct
Customer

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