首页 > 解决方案 > Automapper - 忽略仅存在于 ViewModel 中的属性

问题描述

我正在尝试使用 Automapper 8.1 映射模型 ViewModel,但它不起作用,因为我在 ViewModel 中有我的模型中没有的属性,我收到“未映射成员”异常

模型:

 public class Department
{
    public int Id { get; set; }
    public string DepartmentName { get; set; }
}

视图模型:

 public class DepartmentViewModel
{
    public int Id { get; set; }
    public string DepartmentName { get; set; }
    public int NumberOfEmployees{get;set}

}

映射器配置:

 CreateMap<Department, DepartmentViewModel>()
            .ForMember(d => d.NumberOfEmployees, opt => opt.Ignore());

我可以使它工作的唯一方法是使用 [NotMapped] 注释将 NumberOfEmployees 属性添加到我的模型中。

非常感谢。

标签: asp.net-mvcautomapper

解决方案


推荐阅读