AutoMapper不映射复合对象

时间:2018-04-11 19:16:16

标签: automapper

我正在使用AutoMapper 6.2.2将复合对象(由两个对象组成)映射到一个,但它无法正常工作。

来源/目的地类型

源类型:复合对象(使用来自https://github.com/AutoMapper/AutoMapper/issues/389的建议)

public class JoinedJob 
{
    public JobInfo job { get; set; }
    public LocationInfo loc { get; set; }
}

目的地类型:

public class JobDetailsModel
{
    public int JobID { get; set; }
    public string JobType { get; set; }
}

出于测试目的,我正在使用这个非常简单的目标类,暂时忽略源loc字段。

映射配置

CreateMap<JoinedJob, JobDetailsModel>()
    .ForMember(dest => dest.JobID, opt => opt.MapFrom(src => src.job.JobID))
    .ForMember(dest => dest.JobType, opt => opt.MapFrom(src => src.job.JTName));

预期行为

我希望JoinedJob对象能够正确映射到JobDetailsModel对象。

实际行为

使用ProjectTo&lt;&gt;时,这根本不起作用。我得到的对象有零值和零值。

重现的步骤

所以我测试了一个实例:

var testDetail = _mapper.Map<JoinedJob, JobDetailsModel>(testJob);

抛出此异常:

Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type.    
For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters.
JoinedJob -> JobDetailsModel (Destination member list)

NDSWebAPI.Modules.Core.BusinessLogic.JobRetrieval+JoinedJob -> NDSWebAPI.Models.Core.JobDetailsModel (Destination member list)

Unmapped properties:
    JobID
    JobType

就好像AutoMapper无法识别我定义的.ForMember映射一样?

这应该有用吗?我完全没有映射单个对象的问题(例如,将JobInfo映射到JobDetailsModel工作)。复合对象引入了这个问题。

0 个答案:

没有答案