如何映射以下内容:
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; }
}
我想将PersonEntity
映射到PersonEditVM
。
编辑:喝完一杯咖啡后解决但是如果这是一个更清洁的解决方案会很好吗?
Mapper.CreateMap<PersonEntity, PersonEditVM >()
.ForMember(d=>d.Address, s=>s.MapFrom(p=> new Address{
AddressHome = p.AddressHome,
PostalNumber = p.PostalNumber
}));
答案 0 :(得分:5)
以下是一种方法,可以将您的Adress
(正确拼写为Address
)分成另一个CreateMap
电话:
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));