asp.net-mvc - 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 属性添加到我的模型中。
非常感谢。
解决方案
推荐阅读
- blazor - MudBlazor:单击“确定”按钮时防止关闭 MudDialog
- sharepoint - 无法通过 Graph API 从 sharepoint 在线访问文件
- java - 使用 Java 的 Firestore OR 查询
- react-native - 尝试使用 expo-location 在后台更新位置,但它不起作用
- optimization - TypeError: new(): data must be a sequence (got function)
- ios - 如何在使用条带时在 Firebase 中创建支付意图端点
- redis - 如何在 Azure Redis 缓存中高效缓存大型复杂对象
- uwp - 如何修复 CUIElement::IsRightToLeft 上的 UWP 应用程序崩溃
- android - Android - 无法解析符号“devbrackets”
- sql-server - SQL SERVER 的“SET ROWCOUNT”何时会停止影响 INSERT、DELETE 和 UPDATE 操作?