简体   繁体   中英

Automapper foces subobjects to be mapped

I am trying to map a response object from webservice to a Class in my project. I thought the Automapper will map even the sub objects automatically, it does not unless and until is forcefully set for the member. Why should i do this ?

Mapper.CreateMap<GetIfpQuoteResponse.Quote, QuoteWSModel>() .ForMember(dest => dest.CarrierRate, opt => opt.MapFrom(src => src.Carriers)) .ForMember(dest => dest.DroppedCarriers, opt => opt.MapFrom(src => src.DroppedRates)) .ForMember(dest => dest.MemberPlans, opt => opt.MapFrom(src => src.MemberPlans));

Why Wont the automapper map my su bobjects when i mention the class mapping like this

Mapper.CreateMap<GetIfpQuoteResponse.Quote, QuoteWSModel>(); Mapper.CreateMap<GetIfpQuoteResponse.Quote.Carrier, CarrierRateModel>(); Mapper.CreateMap<GetIfpQuoteResponse.Quote.DroppedCarrier, DroppedCarrierModel>();

AutoMapper only map top level object.

If your class is built in the following way it will not work:

Class A
{
  B b;
}

Class B
{

}

Class A will not know how to map property B inside class A.

To do this you will need to create a Profile class.

Auto Mapper tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM