.net-core - Ignore 和 DoNotValidate 不适用于 AutoMapper 8.1 和 .NET Core 2.1
问题描述
我已经开发了一个天蓝色的功能并.net core
配置automapper
在startup.cs
builder.Services.AddAutoMapper(Assembly.GetAssembly(this.GetType()));
我正在尝试在域类(EF 核心中的表)和 DTO 之间创建映射。域类有一个属性 RowVersion 。我希望在将 dto 映射到域时忽略此属性。
为此,我创建了一个配置文件类并创建了我的自定义地图,但这不起作用。
我试过 DoNotValidate ,但它似乎不起作用
启动.cs
builder.Services.AddAutoMapper(Assembly.GetAssembly(this.GetType()));
public class MapperProfile :Profile
{
public MapperProfile()
{
CreateMap<MyDto, MyDomain>().ForMember(i => i.RowVersion, opt => opt.Ignore());
}
}
服务.cs
_mapper.Map<myDomain>(myDtoInstance)
收到以下错误
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters
Unmapped properties:
[5/28/2019 7:00:40 AM] RowVersion
解决方案
ForMember
用于引用目标对象,在您的情况下,MyDto
.
改用ForSourceMember
:
public MapperProfile()
{
CreateMap<MyDto, MyDomain>().ForSourceMember(i => i.RowVersion, opt => opt.Ignore());
}
推荐阅读
- python - plotly——在箱线图上叠加重要性星/注释
- angular - Angular 8 应用程序中的 ng-select - 与 NgSelectComponent 相关的运行时错误
- javascript - 为什么函数 getPrice() 只调用一次?
- python - 将列表列表转换为字符串列表
- json - 如何在 Python / Flask 中监听 api 更新
- java - 如何使用java将json主体提取为字符串
- c# - 如何将我的代码包装成适当的可等待方法?
- python - 使用 python 从私有 github repo 下载资产
- xamarin.forms - Xamarin.Forms:HTML 数据的文本到语音功能的问题
- yaml - Open API -> 如何在请求或响应正文 YAML 规范中引用类路径/jar 的 Java 对象的外部属性名称/类型