Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I ignore Destination collection type property when mapping using AutoMapper?

Tags:

c#

automapper

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.

like image 292
jbooker Avatar asked Oct 29 '25 17:10

jbooker


1 Answers

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());
like image 112
Engincan Veske Avatar answered Oct 31 '25 08:10

Engincan Veske