0

Despite the fact that I explicitly ignore some props - Automapper maps them because source and destination objects have props with the same name (it is known Automapper behaviour). Logic is usual:

CreateMap<A, B>()
.ForMember(d => d.DataId, opt => opt.MapFrom(s => s.TestDataId))
.ForMember(d => d.MyProp, opt => opt.Ignore());

call:

var res = _mapper.Map<B>(A);

Did you face this behaviour? Is this known limitation?

4
  • it is known Automapper behaviour - please add a link to the source of this to your question Commented Jul 4 at 15:03
  • thanks for sharing this Alexander though it does not answer the question Commented Jul 5 at 10:04
  • @stuartd it is mentined here, for example: docs.automapper.io/en/latest/Flattening.html Commented Jul 5 at 10:07
  • @Ili Welcome to Stack Overflow. Please take the tour to learn how Stack Overflow works and read How to Ask on how to improve the quality of your question. Then edit your question to include your source code as a working minimal reproducible example, which can be compiled and tested by others to provide an answer faster. Commented Jul 5 at 22:30

1 Answer 1

2

I cannot reproduce this issue. Consider the following code:

using AutoMapper;
using Microsoft.Extensions.Logging;

namespace ConsoleApp1;

static class Program
{
    static void Main()
    {
        var loggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); });

        var config = new MapperConfiguration(cfg =>
            cfg.CreateMap<Source, Destination>()
                .ForMember(dest => dest.Y, opt => opt.MapFrom(src => src.C))
                .ForMember(dest => dest.B, opt => opt.Ignore()),
            loggerFactory
        );

        var s = new Source { A = 1, B = 2, C = 3 };
        var m = config.CreateMapper();
        var d = m.Map<Destination>(s);

        Console.WriteLine($"A={d.A}, B={d.B}, Y={d.Y}");
    }
}

public sealed class Source
{
    public int A { get; set; } // Same as Destination
    public int B { get; set; } // Should not be mapped
    public int C { get; set; } // Should be mapped to Destination.Y
}

public sealed class Destination
{
    public int A { get; set; } // Same as Source
    public int B { get; set; } // Should not be mapped
    public int Y { get; set; } // Should be mapped from Source.C
}

The output from this is:

A=1, B=0, Y=3

Which shows that A was mapped from Source.A to Destination.A, Y was mapped from Source.C to Destination.Y and Source.B was not mapped to Destination.B.

NOTE: This was using Automapper version 15.0.0.

[Offtopic: Personally I'd use Mapster (which is free) rather than AutoMapper (which is now commercial)]

Sign up to request clarification or add additional context in comments.

2 Comments

thank you Matthew i use 10.0, maybe it is the problem there, and it was address in later versions?
With AutoMapper v10.0, the opt.Ignore() works. Demo. I don't think it is an AutoMapper issue. Please update the question by providing the real scenario with your actual class and data.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.