如何在 C# 中为子对象使用 AutoMapper?

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

我有两个班级:

public class Data
{
    public string? Property1 { get; set; }
    public string? Property2 { get; set; }
    public int Quantity { get; set; } = 1;
}

public class DataQuantity
{
    public RawData RawData { get; set; } = null!;
    public int Quantity { get; set; } = 1;
}

我想使用 AutoMapper 将“DataQuantity”对象映射到“Data”对象,其中来自 DataQuantity 的数量覆盖来自数据的数量。如果可能的话,我不想命名所有属性。

我试过像这样的简单地图:

CreateMap<DataQuantity, Data>()
    .ForMember(dest => dest.Quantity, opt => opt.MapFrom(src => src.Quantity));

但这行不通。 欢迎任何帮助。

c# asp.net-core .net-core automapper
© www.soinside.com 2019 - 2024. All rights reserved.