Auto-ignore non existing destination properties with AutoMapper

By default, AutoMapper tries to map all properties of the source type to the destination type. If some of the properties are not available in the destination type it will not throw an exception when doing the mapping. However it will throw an exception when you are using ValidateMapperConfiguration().

Imagine if we have the following two types:

class SourceType
{
    public string FirstName { get; set; }
}

class DestinationType
{    
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

ValidateMapperConfiguration() will throw the following exception when you Map SourceType to DestinationType:

AutoMapper.AutoMapperConfigurationException : The following 1 properties on DestinationType are not mapped: 
	LastName 
Add a custom mapping expression, ignore, or rename the property on SourceType.

You can override this behavior by making a small extension method to ignore all properties that do not exist on the target type.

public static IMappingExpression<TSource, TDestination> IgnoreAllNonExisting<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
{
    var sourceType = typeof(TSource);
    var destinationType = typeof(TDestination);
    var existingMaps = Mapper.GetAllTypeMaps().First(x => x.SourceType.Equals(sourceType)
        && x.DestinationType.Equals(destinationType));
    foreach (var property in existingMaps.GetUnmappedPropertyNames())
    {
        expression.ForMember(property, opt => opt.Ignore());
    }
    return expression;
}

Then it is possible to do the mapping as follows:

Mapper.CreateMap<SourceType, DestinationType>()
    .IgnoreAllNonExisting();

It is also possible to customize this method to your needs, by specifically ignoring properties which have a protected or private setter, for example.

Advertisements

3 thoughts on “Auto-ignore non existing destination properties with AutoMapper

  1. Do you happen to know why this isn’t included functionality with AutoMapper? Every time I use AutoMapper I want/need this functionality! Am I wrong in thinking that this should be built in? Or is there a goto package for this extended functionality?

    • It’s been a while since I used AutoMapper but I remember that an exception is only thrown when you validate the configuration. So I suppose it was intended as a means to makes sure that you haven’t missed any properties in your mapping.

  2. Hi Can Gencer,, very nice function it help me a lot. But I would like to know if is there any way to not ignore some properties that are not on the sourcetype but I need on the destination type, for sample: My entity has a property called City. My viewModel has these properties: CityName, CityStateName. Using this function, it ignores the CityName and CityStateName because there is no compatible property on my source type (entity). Thank you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s