5

如何映射以下内容:

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; }
}

我想映射PersonEntityPersonEditVM.

编辑:喝杯咖啡后解决,但如果它是更清洁的解决方案会很好吗?

Mapper.CreateMap<PersonEntity, PersonEditVM >()                
                .ForMember(d=>d.Address, s=>s.MapFrom(p=> new Address{
                    AddressHome = p.AddressHome,
                    PostalNumber = p.PostalNumber
                    }));
4

1 回答 1

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));
于 2012-04-05T12:23:38.143 回答