Automapper complex types mapping exception

前端 未结 1 1299
栀梦
栀梦 2021-01-14 12:28

I\'m trying to implement the AutoMapper for a new module.

I have the MVC model at the web site, I\'m working on, and it looks like this:

public class         


        
相关标签:
1条回答
  • 2021-01-14 13:21

    Thank's to stuartd this are working now!

    The thing that I didn't understand was, that I have to map bottom to top! All the sub-objects have to mapped first, so they would be recognized at the moment parent objects are mapped!

    Now profile's looks like this:

    public class Out: Profile
    {
       protected override void Configure()
        {
            CreateMap<Step1, PersonalDetails>();
            CreateMap<Step2, Phones>();
            CreateMap<Step3, ElectonicCommunication>();
    
            CreateMap<Params, Parameters>();
            CreateMap<Params, RequestContent>()
                .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.RequestId))
                .ForAllMembers(opt => opt.Ignore());
            CreateMap<Steps, RequestContent>()
                .ForMember(dest => dest.PersonalDetails, opt => opt.MapFrom(src => src.Step1))
                .ForMember(dest => dest.Phones, opt => opt.MapFrom(src => src.Step2))
                .ForMember(dest => dest.ElectonicCommunication, opt => opt.MapFrom(src => src.Step3))
                .ForAllMembers(opt => opt.Ignore());
    
            CreateMap<MvcModel, Request>()
                .ForMember(dest => dest.Parameters, opt => opt.MapFrom(src => src.Params))
                .ForMember(dest => dest.RequestContent, opt => opt.MapFrom(src => src.Steps));
        }
    }
    
    0 讨论(0)
提交回复
热议问题