Automapper: IgnoreAllNonExisting support in 5.0

Created on 28 Jun 2016  路  11Comments  路  Source: AutoMapper/AutoMapper

In the last few releases I've been using Mapping extensions like this:

    public static IMappingExpression IgnoreAllNonExisting(this IMappingExpression expression)
    {
        foreach (var property in expression.TypeMap.GetUnmappedPropertyNames())
        {
            expression.ForMember(property, opt => opt.Ignore());
        }
        return expression;
    }

    public static IMappingExpression IgnoreAllNonExistingSource(this IMappingExpression expression)
    {
        foreach (var property in expression.TypeMap.GetUnmappedPropertyNames())
        {
            expression.ForSourceMember(property, opt => opt.Ignore());
        }
        return expression;
    }

Which I use in my configuration like so:

    CreateMap(typeof(TempMapViewModel), typeof(MapViewModel))
            .IgnoreAllNonExisting()
            .IgnoreAllNonExistingSource()
            .ForAllMembers(opt => opt.Condition(srs => !srs.IsSourceValueNull));

This currently doesn't work because expression.TypeMap is no longer available. Is there a better alternative in 5.0?

Most helpful comment

No, it doesn't. It only throws an exception during configuration validation, never at runtime for missing members.

I think you need CreateMap<Source, Dest>(MemberList.Source), this uses the source type as validation.

All 11 comments

What are you trying to do with all those ignores?

I have properties in MapViewModel that you could consider required, and I have nullable properties in TempMapViewModel that are optional. I want to take all the properties in TempMapViewModel that are not null and merge them into MapViewModel.

No I meant why are you ignoring all those members?

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.

I override this behavior with the extension method to ignore all properties that do not exist on the target type.

No, it doesn't. It only throws an exception during configuration validation, never at runtime for missing members.

I think you need CreateMap<Source, Dest>(MemberList.Source), this uses the source type as validation.

Yes that's what I meant, no exception during runtime, but an exception during config validation.

Your suggestion is precisely what I was looking for, thanks.

Now that 5.0.1 had come out I had to change this to CreateMap<Source, Dest>(MemberList.None) otherwise I would get the error:

No available constructor.

A failing test?

MemberList.None appears to have no affect on behavior for me in 6.0.2 I had the following 4.2 code that would map a single property from an object that has hundreds:

var map = CreateMap();
map.ForAllMembers(x => x.Ignore());
map.ForMember(x => x.Prop1, o => o.MapFrom(x => x.Prop1))

The "IgnoreAll" now has the behavior of ignoring even though I have set a mapping for it after ignoring. I tried adding MemberList.None to the CreateMap call and removing the "IgnoreAll" but it is throwing an error on properties I don't care about.

Edit: this works in 6.0.2
CreateMap()
ForMember(x => x.Prop1, o => o.MapFrom(x => x.Prop1))
.ForAllOtherMembers(x => x.Ignore())

Is there any way to do this inside of the Typeconverter that a Profile use? MyTypeConverter class implements ITypeConverter?

Profile
{
CreateMap(destination, model).ConvertUsing(MyTypeConverter);
}

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dmdymov picture dmdymov  路  3Comments

Mds92 picture Mds92  路  5Comments

vivainio picture vivainio  路  6Comments

colin-young picture colin-young  路  6Comments

jepl4052 picture jepl4052  路  6Comments