1
votes

I'm mapping from an input model (source) to a domain model (destination)

The destination class has a list of objects of a type and that type has properties which are not on the source list type

This is causing the following exception

AutoMapperConfigurationException: Unmapped members were found. Review the types and members below.

Oddly enough, no exceptions are throw when the input models collection object type has a property which is not contained within the domain models collection object type. Its only the other way around.

How can I ensure that properties can exist on the domain models collection object type which are not mapped to anything?

Here is MRE https://github.com/jstallm/AutomapperListIssue-MRE

In this MRE, you can see that the input model has list of type Class1InputModel. Class1InputModel has a property of Description which automapper does not complain about at all.

However, the class DomainModel has a list of type Class1DomainModel which contains field named Other. This is the only property causing issues. Essentially upon successfully mapping, I want the property "Other" to be null.

3

3 Answers

1
votes

The place where you define mapping, like

CreateMap<Source, Destination>();

You can ignore the specific field under below

CreateMap<Source, Destination>().ForMember(x => x.Destination, opt => opt.Ignore());
1
votes

You can ignore this list :

Mapper.CreateMap<SourceType, DestinationType>()
    .ForMember(dst => dst.SpecialProperty, opt => opt.Ignore());

Other options is to add map for the list item type of each one and ignore only the list type missing properties and then you dont need to ignore the list in the parents mapping, for example :

Mapper.CreateMap<SourceType, DestinationType>();

Mapper.CreateMap<SourceListItemsType, DestinationListItemsType>()
    .ForMember(dst => dst.SpecialProperty1, opt => opt.Ignore()
    .ForMember(dst => dst.SpecialProperty2, opt => opt.Ignore()
    .ForMember(dst => dst.SpecialProperty3, opt => opt.Ignore()
    .ForMember(dst => dst.SpecialProperty4, opt => opt.Ignore());
0
votes

The exception is no longer thrown just by the virtue of creating a simple mapping between the lists themselves. No ignores needed.

Just add a profile for the lists themselves enter image description here

Then ensure it is added to mapperconfiguration

enter image description here