2017-08-29 81 views
1

如何使用Automapper設置嵌套對象的內部屬性? 股東擁有類聯繫人的內部財產。 屬性PhysicalOrPostalAddresses需要映射到Individual.Address或Corporate.Addresses。Automapper設置內部對象屬性

var holderMap = CreateMap<Shareholder, Trustee>(MemberList.None); 
     holderMap 
      .ForMember(dest => dest.Individual, opt => 
     { 
      opt.Condition(src => src.Person != null); 
      opt.MapFrom(src => src.Person); 
     }) 
     .ForMember(dest => dest.Corporate, opt => 
     { 
      opt.Condition(src => src.Organisation != null); 
      opt.MapFrom(src => src.Organisation); 
     }) 
     // this code doesn't work 
     .ForMember(dest => dest.Corporate.Addresses, o => 
     { 
      o.Condition(s => s.Organisation != null); 
      o.MapFrom(s => s.Contacts.PhysicalOrPostalAddresses); 
     }) 
     .ForMember(dest => dest.Individual.Addresses, o => 
     { 
      o.Condition(s => s.Person != null); 
      o.MapFrom(s => s.Contacts.PhysicalOrPostalAddresses); 
     }) 
    .ReverseMap(); 

回答

0

您需要使用ForPath而不是ForMember。

+0

謝謝!有用。如何爲ForMember指定條件'opt.Condition(src => src.Organisation!= null);' – Ma3yTa

+0

現在不支持。你可以在github上請求它。 –