我正在使用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
工作)。复合对象引入了这个问题。