首页 > 解决方案 > Ignore 和 DoNotValidate 不适用于 AutoMapper 8.1 和 .NET Core 2.1

问题描述

我已经开发了一个天蓝色的功能并.net core配置automapperstartup.csbuilder.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

标签: .net-coreautomapperazure-functions

解决方案


ForMember用于引用目标对象,在您的情况下,MyDto.

改用ForSourceMember

public MapperProfile()
{
     CreateMap<MyDto, MyDomain>().ForSourceMember(i => i.RowVersion, opt => opt.Ignore());
}

推荐阅读