wondra
wondra

Reputation: 3583

Automapper how to use IgnoreMap attribute with MemberList.Source option

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.

Upvotes: 1

Views: 1770

Answers (2)

Mortaza Ghahremani
Mortaza Ghahremani

Reputation: 392

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());

Upvotes: 0

Gabriel Llorico
Gabriel Llorico

Reputation: 1803

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?

Upvotes: 1

Related Questions