自动映射复杂类型映射异常

时间:2016-04-13 15:01:50

标签: c# .net 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));
    }
}

这些是示意性相似的,虽然真实对象有点大,但名称不同。

期望: MvcModel要映射到Request及其后退。

更准确:

  1. MvcModel.Params要映射到Request.Parameters并向后
  2. MvcModel.Steps要映射到Request.RequestContent并向后
  3. InnerIdSessionClerk,来自Private的{​​{1}}必须被忽略
  4. 问题:我在应用程序启动时遇到错误:

      

    无法映射......上的以下属性:
      添加自定义映射表达式,忽略,添加自定义解析程序或修改   目的地类型......   语境:   映射到属性......从......到......   从类型映射到...   源类型AutoMapperMessageBugTests.SourceChild映射到类型   抛出了“AutoMapper.AutoMapperConfigurationException”类型的异常。

    我没有找到关于如何在应用程序中全局使用AutoMapper的好文档,而且无法弄清楚如何正确使用它以及我究竟做错了什么。

    我找不到映射以太的复杂对象的好例子......

    有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

感谢 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));
    }
}