I have 3 classes:
public class CountryModel
{
public int Id { get; set; }
public string Title { get; set; }
}
public class CountryDTO
{
public int Id { get; set; }
public string Title { get; set; }
}
public class BaseCountryDTO
{
public CountryDTO Country {get; set};
}
I need to map CountryDTO to CountryModel, but through BaseCountryDTO class.
I know that I can do it like this:
CreateMap<BaseCountryDTO, CountryModel>()
.ForMember(model => model.Id, o => o.MapFrom(dto => dto.Country.Id))
.ForMember(model => model.Title, o => o.MapFrom(dto => dto.Country.Title));
But I want to do it clear, something like this:
// This is not working code, just my imagination :)
CreateMap<BaseCountryDTO, CountryModel>()
.ForMember(model => model, dto => dto.Country));
Because in model can be more than 2 properties. Is there way to do it?
question from:
https://stackoverflow.com/questions/65910732/c-sharp-how-to-map-inner-property-object-to-outer-class-with-automapper 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…