c# - Automapper 如何将 IgnoreMap 属性与 MemberList.Source 选项一起使用
问题描述
如何将基于属性的成员忽略(例如[IgnoreMap]
)与MemberList.Source
Automapper 9.0 中的选项结合使用?IgnoreMap 属性似乎被...忽略 - 以下示例抛出:
public class Source
{
public string PropertyA { get; set; }
[IgnoreMap]
public string IgnoredProperty { get; set; }
}
public class Destination
{
public string PropertyA { get; set; }
public string PropertyC { get; set; }
}
public class MyProfile : Profile
{
public MyProfile()
{
CreateMap<Source, Destination>(MemberList.Source);
}
}
配置时使用MapperConfiguration.AssertConfigurationIsValid()
. 它抛出 AssertConfigurationIsValid 就好像[IgnoreMap]
不存在一样:
未映射的成员被发现。查看下面的类型和成员。添加自定义映射表达式、忽略、添加自定义解析器或修改源/目标类型对于没有匹配的构造函数,添加无参数 ctor、添加可选参数或映射所有构造函数参数 ======= ==================================================== ========= Source -> Destination(源成员列表) Mapping.Source -> Mapping.Destination(源成员列表)
未映射的属性:IgnoredProperty
我也尝试过[Ignore]
[NotMapped]
属性,但结果是一样的。
解决方案
我没有尝试过自动映射器的属性忽略,我总是使用这个
CreateMap<Source, Destination>.ForMember(x => x.IgnoredProperty, opt => opt.Ignore());
推荐阅读
- java - Java中的匹配游戏
- anchor - 如何在音频播放时自动滚动到锚点
- angular - 如何按未在显示的列中声明的属性对角度材料表行进行排序
- go - 无法抓取数据
- c# - 需要使用 C# 在文本框中输入的自动完成来搜索子字符串或任何匹配的字符串
- amazon-web-services - 无法通过 Site-to-Site VPN 和 DMS 源数据库端点测试连接从本地 ping DMS 复制实例的私有 IP
- java - 编写方法来查找我的 ArrayList 中最大整数的索引时遇到问题
- react-native - 如何在odoo消息中添加附件ID
- oauth-2.0 - LinkedIn API v2 是否符合 OAuth 2.0 规范?
- python - 将此 QuerySet 解析为元组时导致效率低下的原因是什么?