1

How to use attribute-based member ignoring (e.g. [IgnoreMap]) in conjunction with the MemberList.Source option in Automapper 9.0? The IgnoreMap attribute seems to be ...ignored - the following example throws:

public class Source
{
    public string PropertyA { get; set; }
    [IgnoreMap]
    public string IgnoredProperty { get; set; }
}

public class Destination
{
    public string PropertyA { get; set; }

    public string PropertyC { get; set; }
}

public class MyProfile : Profile
{
    public MyProfile()
    {
        CreateMap<Source, Destination>(MemberList.Source);
    }
}

when configuring using the MapperConfiguration.AssertConfigurationIsValid(). It throws AssertConfigurationIsValid as if [IgnoreMap] was not there:

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 ================================================================== Source -> Destination (Source member list) Mapping.Source -> Mapping.Destination (Source member list)

Unmapped properties: IgnoredProperty

I have also tried [Ignore] [NotMapped] attributes, however the result was the same.

wondra
  • 3,271
  • 3
  • 29
  • 48

2 Answers2

1

i havent tried attribute ignore for automapper, i always use this

CreateMap<Source, Destination>.ForMember(x => x.IgnoredProperty, opt => opt.Ignore());

or try answers here : How to configure Automapper to automatically ignore properties with ReadOnly attribute?

Gabriel Llorico
  • 1,783
  • 1
  • 13
  • 23
  • Thanks, but this cannot work - `IgnoredProperty` is not a member of `Destination` class. – wondra Oct 21 '19 at 09:46
  • @wondra then you can switch which property should the `IgnoredProperty` should be handled – Gabriel Llorico Oct 21 '19 at 14:30
  • I think we misunderstood eachother, the `x` in you example is of `Destination` Type, not a `Source` Type. Clearly `x` cannot be used to ignore the `Source.IgnoredProperty` property, or in other words: the sample would not compile. – wondra Oct 21 '19 at 16:45
0

Additional properties in source automatically ignored by mapper ,so you should ignore explicitly ignored properties of destination, so in your case you must do like below code:

CreateMap<Source, Destination>.ForMember(x => x.PropertyC , options => options.Ignore());
  • `PropertyC` is not the one to be ignored, Automapper complains about `IgnoredProperty` property. – wondra Oct 21 '19 at 13:02