Automapper复杂类型映射异常

我试图为一个新模块实现AutoMapper。

我在网站上有MVC模型,我正在研究,看起来像这样:

public class MvcModel
{
    public Params Params { get; set; }
    public Steps Steps { get; set; }
}    
public class Params
{
    public int? RequestId { get; set; } 
    public bool NewClient { get; set; }
}    
public class Steps
{
    public Step1 Step1 { get; set; }    
    public Step2 Step2 { get; set; }    
    public Step3 Step3 { get; set; }    
}    
public class Step1
{
    public int Name { get; set; }   
}    
public class Step2
{
    public int Phone { get; set; }  
}    
public class Step3
{
    public int Email { get; set; }  
}

另一方面,我有下一个课程,我必须通过一些服务:

public class Request
{
    public Parameters Parameters { get; set; }
    public RequestContent RequestContent { get; set; }
}

public class Parameters
{
    public int NewClient { get; set; }
}

public class RequestContent
{
    public int Id { get; set; } 
    public int InnerId { get; set; }
    public string Session { get; set; }
    public string Clerk { get; set; }
    public bool Private { get; set; }

    public PersonalDetails PersonalDetails { get; set; }    
    public Phones Phones { get; set; }  
    public ElectonicCommunication ElectonicCommunication { get; set; }  
}

public class PersonalDetails
{
    public int Name { get; set; }   
}

public class Phones
{
    public int Phone { get; set; }  
}

public class ElectonicCommunication
{
    public int Email { get; set; }  
}

我创建了一个新的AutoMapper配置类(从Global.asax调用):

public class AutoMapperConfig
{
    public static void Configure()
    {
        MapperConfiguration MapperConfiguration = new MapperConfiguration(cfg => {
            cfg.AddProfile<Out>();
            cfg.AddProfile<In>();
            cfg.CreateMap<MvcModel, Request>();
        });
        MapperConfiguration.AssertConfigurationIsValid();
    }
}

public class Out: Profile
{
    protected override void Configure()
    {
        CreateMap<MvcModel, Request>()
            .ForMember(dest => dest.Parameters, opt => opt.MapFrom(src => src.Params))
            .ForMember(dest => dest.RequestContent, opt => opt.MapFrom(src => src.Steps));
    }
}

public class In: Profile
{
    protected override void Configure()
    {
        CreateMap<Request, MvcModel>()
            .ForMember(dest => dest.Params, opt => opt.MapFrom(src => src.Parameters))
            .ForMember(dest => dest.Steps, opt => opt.MapFrom(src => src.RequestContent));
    }
}

这些都是示意性的类似,但实际的对象有点大,名称不同。

Excipationations: MvcModel被映射到RequestMvcModel

为了更准确:

  • MvcModel.Params映射到Request.Parameters并向后
  • MvcModel.Steps被映射到Request.RequestContent并向后
  • InnerIdSessionClerkRequest.RequestContent Private必须被忽略
  • 问题:在应用程序启动时出现错误:

    无法映射以下属性...:
    添加自定义映射表达式,忽略,添加自定义解析器或修改目标类型...上下文映射到属性...从...到...从源类型映射到类型... AutoMapperMessageBugTests.SourceChild映射到抛出类型'AutoMapper.AutoMapperConfigurationException'的异常。

    我没有找到关于如何在应用程序中使用AutoMapper的很好的文档,只是不知道如何正确使用它,以及我在做什么错误。

    我无法找到复杂对象映射的好例子......

    有人可以帮我弄这个吗?


    感谢stuartd这正在工作!

    我不明白的是,我必须从下到上映射! 所有的子对象都必须首先映射,以便在父对象映射的时候识别它们!

    现在个人资料看起来像这样:

    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));
        }
    }
    
    链接地址: http://www.djcxy.com/p/37393.html

    上一篇: Automapper complex types mapping exception

    下一篇: Complex Automapper Configuration