How to ignore all destination items except those that are displayed?

Is there any way to do this? We have a SummaryDto that displays three different types, and when we create a map for each type, the details that are not displayed cause an error. The dto summary has about 35 attributes. Using the Ignore () option for each of them is too big a problem. Is there global neglect? Something like

CreateMap<Source,Target>() .IgnoreAllUnmapped(); 
+53
c # automapper
Dec 06 '10 at 14:50
source share
5 answers

This works for me:

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

Since ForAllMembers returns void , calling ForAllMembers(o => o.Ignore()) will not work without this extension method. We want to keep the display expression available for later display:

 CreateMap<Source, Destination>() .IgnoreAllUnmapped() .ForMember(d => d.Text, o => o.MapFrom(s => s.Name)) .ForMember(d => d.Value, o => o.MapFrom(s => s.Id)); 
+89
Nov 09 '11 at 20:17
source share

I struggled with this for too long, or at least a problem like this. I had a class with many properties (about 30), and I just wanted to display about 4 of them. It seems crazy to add 26 ignored statements (especially when it means that future changes to the class mean they need to be updated!)

I finally found that I can tell AutoMapper to ignore everything, and then explicitly add the ones I need.

 // Create a map, store a reference to it in a local variable var map = CreateMap<Source,Target>(); // Ignore all members map.ForAllMembers(opt => opt.Ignore()); // Add mapping for P1 map.ForMember(dest => dest.P1, opt => opt.MapFrom( src => src.P1)); // Add other mappings... map.ForMember(dest => dest.P2, opt => opt.MapFrom( src => src.P2)); map.ForMember(dest => dest.P3, opt => opt.MapFrom( src => src.P3)); map.ForMember(dest => dest.P4, opt => opt.MapFrom( src => src.P4)); 

You will be forgiven to think that you can just do this (but not because it will not compile):

 // This won't compile CreateMap<Source,Target>() .ForAllMembers(opt => opt.Ignore()) .ForMember(dest => dest.P1, opt => opt.MapFrom( src => src.P1)); 

The reason this does not work is because the ForAllMembers () method does not support free chaining (at least in the current version 2.0).

The good news is that the chainless version really works. Of course, one of the caveats is that you need to explicitly tell AutoMapper which members should be displayed. I have not yet found an easy way to have it in both directions, so that you can still use the implicit mappings and ignore the broken ones.

+22
Oct 17 '11 at 10:27
source share

To avoid explicitly specifying the displayed properties, you can use IgnoreAllNonExisting . It ignores any destination properties that do not have display properties for the source.

+5
Mar 08 '13 at 22:45
source share

Try using .ConvertUsing() in your case, for example.

 CreateMap<Source,Target>() .ConvertUsing(converter=> new Target(){ P1 = converter.P1, .... }); 

So, you can describe all the properties that you want to have in your object, others will be ignored.

+1
Jan 17 '11 at 12:10
source share

An extension method that allows you to freely use the syntax of the ForAllMembers method:

 public static IMappingExpression<TSource, TDestination> IgnoreAllMembers<TSource, TDestination>( this IMappingExpression<TSource, TDestination> expression ) { expression.ForAllMembers(opt => opt.Ignore()); return expression; } 

Using:

The IgnoreAllMembers call must be before the ForMember call.

  CreateMap<LocationRevision, Dto.LocationAddressMap>() .IgnoreAllMembers() .ForMember(m => m.LocationId, opt => opt.MapFrom(src => src.Id)) ; 
+1
Feb 19 '15 at 23:16
source share



All Articles