How to skip the mapping between properties of a source and a target object in AutoMapper

As I did in my previous article, I focus again on the AutoMapper framework.

When I map one object to another, I often deal with a destination object that contains LESS properties than the source object. If I take no action, an exception is going to be thrown. For that we will have to declare the skipped properties by using the DoNotValidate method when we define the mapping (CreateMap) between the two objects:

1
2
    CreateMap<SourceObject, DestinationObject>()
        .ForSourceMember(source => source.Description , config => config.DoNotValidate());

In the case where the destination object contains MORE properties than the source object, we can use the Ignore method:

1
2
    CreateMap<SourceObject, DestinationObject>()
        .ForMember(dest => dest.Description , expr => expr.Ignore());

I hope this is going to help some of you, since chances are you will have to deal with mapping of objects that are do not contain the same number of properties.

comments powered by Disqus