1

I am new to AutoMapper and wanting to setup for two properties to map but the other 15 ignore.

My research and reading such as here indicates 3 options.

  1. Use Ignore() such as: (currently do and it works but is bloated)

    d => d.IgnoreMe, opt => opt.Ignore()
    
  2. Annotate with [IgnoreMapAttribute]. Wish I could but the object is not mine to tweak.

  3. Extension Method such as seen here answer posted by Rami A(I would have to tweak it)

However it seemed to me these options are work arounds and I am just not using AutoMapper properly?

What I am looking for is something like:

Mapper.CreateMap<Customer, DropPointModel>()
      .ForMember(dest => dest.CustomerId,
                 opts => opts.MapFrom(src => src.Id))
      .ForMember(dest => dest.VendorId,
                 opts => opts.MapFrom(src => src.VendorId))
      .IgnoreAllOtherMappings();

Does something along these lines exist in AutoMapper and I'm not implementing correctly or am I correct and just need to write the extension method. If the latter I will post back here for others.

Community
  • 1
  • 1
GPGVM
  • 5,515
  • 10
  • 56
  • 97

2 Answers2

0

Have you tried using somethin like the following

public static class MappingExpressionExtensions
{
    public static IMappingExpression<TSource, TDest> IgnoreAllUnmapped<TSource, TDest>(this IMappingExpression<TSource, TDest> expression)
    {
        expression.ForAllMembers(opt => opt.Ignore());
        return expression;
    }
}

      .ForMember(dest => dest.CustomerId,
                       opts => opts.MapFrom(src => src.Id))
      .ForMember(dest => dest.VendorId,
                      opts => opts.MapFrom(src => src.VendorId))
        .IgnoreAllUnmapped();

or this

.ForMember(dest => dest.CustomerId,
                       opts => opts.MapFrom(src => src.Id))
      .ForMember(dest => dest.VendorId,
                      opts => opts.MapFrom(src => src.VendorId))
      .ForAllMembers(opt => opt.Ignore());
COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
  • See I figured I wasn't understanding correctly. My understanding of ForAllMembers was that it would apply to all member retroactively so the previous two mappings (CustomerId & VendorId) would be ignored. – GPGVM Mar 26 '15 at 13:26
  • and as a follow up testing ForAllMembers(opts => opts.Ignore()) does indeed retroactively ignore everything. – GPGVM Mar 26 '15 at 15:08
  • your answer is a good one if we reverse the order. Do the ignore first then do the specific mappings. – GPGVM Mar 27 '15 at 19:24
0

This is a duplicate question to another and I am not going to provide the answer here as the original author(s) need credit for their work.

Check it out here and be sure to upvote if it helped.

AutoMapper: "Ignore the rest"?

Community
  • 1
  • 1
GPGVM
  • 5,515
  • 10
  • 56
  • 97