How can I map the following:
public abstract class PersonBase
{
public Address Address { get; set; }
}
public class Address
{
public string AddressHome { get; set; }
public int PostalNumber { get; set; }
}
public class PersonEditVM : PersonBase
{
public int PersonId { get; set; }
}
public PersonEntity
{
public int PersonId { get; set; }
public string AddressHome { get; set; }
public int PostalNumber { get; set; }
}
I would like to map PersonEntity
to PersonEditVM
.
EDIT: Solved after a cup of coffee but it would be nice if it's a cleaner solution?.
Mapper.CreateMap<PersonEntity, PersonEditVM >()
.ForMember(d=>d.Address, s=>s.MapFrom(p=> new Address{
AddressHome = p.AddressHome,
PostalNumber = p.PostalNumber
}));
Here's one way to do it, which would split your Adress
(which is correctly spelled Address
by the way) mapping into another CreateMap
call:
Mapper.CreateMap<PersonEntity, PersonEditVM>()
.ForMember(dest => dest.Adress, opt => opt.MapFrom(src => src));
Mapper.CreateMap<PersonEntity, Adress>()
.ForMember(dest => dest.AdressHome, opt => opt.MapFrom(src => src.AdressHome))
.ForMember(dest => dest.PostalNumber, opt => opt.MapFrom(src => src.PostalNumber));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With