I'm using AutoMapper 6. Consider the following classes:
public class Source
{
public FlattenableClass Flattenable { get; set; }
public List<EmailAddress> EmailAddresses { get; set; }
}
public class Destination
{
public string FlattenableProp1 { get; set; }
public string FlattenableProp2 { get; set; }
public MappedEmailAddress EmailAddress1 { get; set; }
}
where FlattenableClass
has properties named Prop1
and Prop2
. As you can see, the source has a collection of EmailAddress
but the destination only needs the first one because although our database allows a collection of email addresses, the application is going to support one. I believe I can arrange this mapping from Source
to Destination
like so:
CreateMap<Source, Destination>()
.ForMember(d => d.EmailAddress1, opt => opt.ResolveUsing(s => s.EmailAddresses?.FirstOrDefault()));
but, unsurprisingly, if I then call ReverseMap()
on that, it doesn't know how to map EmailAddress1
back into the EmailAddresses
collection. Is there any way to get it to map EmailAddress1
back to an EmailAddress
and add it to the EmailAddresses
collection?