【问题标题】:Mapping with automapper to an object with one additional field results in exception使用自动映射器映射到具有一个附加字段的对象会导致异常
【发布时间】:2017-04-11 01:05:57
【问题描述】:

您好,我正在使用 autommapper 映射对象之间的对象,我有一种情况,我必须映射到一个包含另一个属性的对象,然后它是源。当我尝试运行程序时,我得到了这个错误:

{"\n找到未映射的成员。查看下面的类型和成员。\n添加自定义映射表达式、忽略、添加自定义解析器或修改源/目标类型\n========= ==================================================== ==================================================== ==\r\nGetUpcomingLessons_Result -> UpcomingLessonDTO(目标成员列表)\r\neConnect.Model.GetUpcomingLessons_Result -> eConnect.DomainServices.Contracts.DTOs.Dashboard.UpcomingLessonDTO(目标成员列表)\r\n------ -------------------------------------------------- -------------------------------------------------- -----\r\n教师官方名称\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 为这两个对象创建映射?

【问题讨论】:

    标签: c# automapper


    【解决方案1】:

    试试这个:

    Mapper.CreateMap<GetUpcomingLessons_Result, UpcomingLessonDTO>()
    .ForMember(dest => dest.TeacherOfficialName, opt => opt.Ignore());
    

    【讨论】:

    • 谢谢,如果我想为 TeacherOfficialName 添加一个值,我将如何进行?
    • 您可以在调用 Map 后自己分配值,也可以配置 AutoMapper 以使用投影提供值:有关示例,请参见 github.com/AutoMapper/AutoMapper/wiki/Projection
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    相关资源
    最近更新 更多