简体   繁体   English

使用忽略属性时,AutoMapper ForMember不起作用

[英]AutoMapper ForMember not working when using ignore properties

I'm using auto mapper to map two objects, but when I call 我正在使用自动映射器来映射两个对象,但是当我调用

Mapper.Map<PropertyDto>(CreatePropertyRequestDto, property)

an exception is thrown saying 抛出一个异常说

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 ==================================================================================================================================== AutoMapper created this type map for you, but your types cannot be mapped using the current configuration. 添加自定义映射表达式,忽略,添加自定义解析器或修改源/目标类型对于没有匹配的构造函数,请添加no-arg ctor,添加可选参数或映射所有构造函数参数======= ================================================== ================================================== ========================= AutoMapper为您创建了此类型映射,但是无法使用当前配置映射您的类型。 CreatePropertyRequestDto -> PropertyDto (Destination member list) PropertyHippo.Properties.Shared.HttpRequestResponse.Dto.CreatePropertyRequestDto -> PropertyHippo.Properties.Shared.Dto.PropertyDto (Destination member list) CreatePropertyRequestDto-> PropertyDto(目标成员列表)PropertyHippo.Properties.Shared.HttpRequestResponse.Dto.CreatePropertyRequestDto-> PropertyHippo.Properties.Shared.Dto.PropertyDto(目标成员列表)

Unmapped properties: PropertyId Guid CreateDate UpdateDate LastEditedBy GuidString 未映射的属性:PropertyId Guid CreateDate UpdateDate LastEditedBy GuidString

Below is my configuration. 下面是我的配置。

    CreateMap<CreatePropertyRequestDto, PropertyDto>()
        .ForMember(dest => dest.PropertyId, opt => opt.Ignore())
        .ForMember(dest => dest.Guid, opt => opt.Ignore())
        .ForMember(dest => dest.CreateDate, opt => opt.Ignore())
        .ForMember(dest => dest.UpdateDate, opt => opt.Ignore())
        .ForMember(dest => dest.LastEditedBy, opt => opt.Ignore())
        .ForMember(dest => dest.GuidString, opt => opt.Ignore());

I have searched for the answer and found this and this and in the docs but I still can't see what I'm doing wrong. 我已经搜索了答案,并在文档中找到了这个这个 ,但是我仍然看不到我在做什么错。

What am I missing? 我想念什么?

EDIT 编辑

Added the breakpoint and can see that block of code is being hit. 添加了断点,可以看到正在击中代码块。 As a test I also removed the offending properties and can see the mapping works as expected 作为测试,我还删除了有问题的属性,可以看到映射按预期工作

@Progman thank you for the suggestion to create an MCVE. @Progman感谢您提出创建MCVE的建议。 While creating an MCVE I had a problem initalising the mapper and found this answer . 创建MCVE时,我在初始化映射器时遇到问题,并找到了答案

My problem was that I was using a static method Mapper.Map and I should have been injecting a type of IMapper (while writing this answer I see that my question missed how the mapperConfigurations were being set). 我的问题是我使用的是静态方法Mapper.Map,并且应该注入一种IMapper(在编写此答案时,我看到我的问题错过了如何设置mapperConfigurations)。

Startup.cs Startup.cs

public void ConfigureServices(IServiceCollection services)
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.AddProfile(new AutoMapperConfig());
            cfg.AddProfile(new SqlAutoMapperConfig());
        });

        services.AddSingleton(config.CreateMapper());

        services.AddMvc();

        _container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();

        _container.Register<IMapper>(() => mapperConfig.CreateMapper(_container.GetInstance));
    }

Handler.cs Handler.cs

public class PropertyRequestHandler : IRequestHandler<NewPropertyRequest, string>
    {
        public PropertyRequestHandler(IPropertyManager propertyManager, IMapper mapper)
        {
            Ensure.That(mapper).IsNotNull();

            _mapper = mapper;
        }

        private IMapper _mapper { get; }

        public string Handle(NewPropertyRequest message)
        {
            var newProperty = _mapper.Map<PropertyDto>(message.NewProperty);
            ...other stuff...
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM