tony09uk
tony09uk

Reputation: 2991

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. CreatePropertyRequestDto -> PropertyDto (Destination member list) PropertyHippo.Properties.Shared.HttpRequestResponse.Dto.CreatePropertyRequestDto -> PropertyHippo.Properties.Shared.Dto.PropertyDto (Destination member list)

Unmapped properties: 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

Upvotes: 1

Views: 1298

Answers (1)

tony09uk
tony09uk

Reputation: 2991

@Progman thank you for the suggestion to create an MCVE. While creating an MCVE I had a problem initalising the mapper and found this answer.

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).

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

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...
        }
    }

Upvotes: 2

Related Questions