您好我正在使用自动映射器来处理对象之间的问题,我有一个案例,我必须maping到一个包含一个属性的对象然后它是source.WHeren我尝试运行程序我得到这个错误:
找到了{“\ n未映射的成员。请查看下面的类型和成员。\ n添加自定义映射表达式,忽略,添加自定义解析程序或修改源/目标类型\ n ========= ================================================== ================================================== == \ r \ nGetUpcomingLessons_Result - > UpcomingLessonDTO(目标成员列表)\ r \ neConnect.Model.GetUpcomingLessons_Result - > eConnect.DomainServices.Contracts.DTOs.Dashboard.UpcomingLessonDTO(目标成员列表)\ r \ n ---- -------------------------------------------------- -------------------------------------------------- ------- \ r \ nTeacherOfficialName \ r \ n“个}
但是这个错误说的不可能是因为我添加了这两个对象的映射:
Mapper.CreateMap<GetUpcomingLessons_Result, UpcomingLessonDTO>();
当我添加额外的字段TeacherOfficialName时,错误才会生效。
这是我试图映射的女巫的代码:
public partial class GetUpcomingLessons_Result
{
public string CourseName { get; set; }
public string ModuleName { get; set; }
public Nullable<int> ModuleInstanceId { get; set; }
public int StudentAssignmentId { get; set; }
public int StudentAssignmentInstanceId { get; set; }
public Nullable<System.DateTime> EventDate { get; set; }
public Nullable<System.DateTime> StartTime { get; set; }
public Nullable<System.DateTime> EndTime { get; set; }
public string TeacherLastName { get; set; }
public string TeacherMiddleName { get; set; }
public string TeacherGender { get; set; }
public Nullable<int> LessonNumber { get; set; }
public string LocationName { get; set; }
}
这是我试图映射的女巫的代码:
public class UpcomingLessonDTO
{
public string CourseName { get; set; }
public string ModuleName { get; set; }
public int? ModuleInstanceId { get; set; }
public int StudentAssignmentId { get; set; }
public int StudentAssignmentInstanceId { get; set; }
public DateTime? EventDate { get; set; }
public DateTime? StartTime { get; set; }
public DateTime? EndTime { get; set; }
public string TeacherLastName { get; set; }
public string TeacherMiddleName { get; set; }
public string TeacherGender { get; set; }
public int? LessonNumber { get; set; }
public string LocationName { get; set; }
// additional fields
public string TeacherOfficialName { get; set; }
}
如何使用automapper为这两个对象创建映射?
答案 0 :(得分:1)
试试这个:
Mapper.CreateMap<GetUpcomingLessons_Result, UpcomingLessonDTO>()
.ForMember(dest => dest.TeacherOfficialName, opt => opt.Ignore());