I do have two classes:
public class Data
{
public string? Property1 { get; set; }
public string? Property2 { get; set; }
public int Quantity { get; set; } = 1;
}
public class DataQuantity
{
public Data Data { get; set; } = null!;
public int Quantity { get; set; } = 1;
}
I want to use AutoMapper to map a "DataQuantity"-object to a "Data"-opject, where the Quantity from DataQuantity overwrites the Quantity from Data. If possible, I don't want to name all properties.
I've tried simple maps like:
CreateMap<DataQuantity, Data>()
.ForMember(dest => dest.Quantity, opt => opt.MapFrom(src => src.Quantity));
In my service I call it like this:
var data = _mapper.Map<Data>(dataqty);
In "dataqty" all props are filled. After the mapping "data" props are all null or default.
dotnetfiddle: https://dotnetfiddle.net/5DZopv
Any help is welcome.
You can use IncludeMembers
to perform "customized" flattening:
cfg.CreateMap<Data, Data>();
cfg.CreateMap<DataQuantity, Data>().IncludeMembers(s => s.Data);