1

I've got the following DataModels:

class TicketDM
{
 public int Id{get;set;}
 public List<NoteContractDM> Notes{get;set;}
}

class NoteContractDM
{
 public NoteDM Note{get;set;}
}

class NoteDM{
 public string Subject{get;set;}
 public string Description{get;set}
}

And the following ViewModels:

public class TicketVM
{
 public int Id {get;set;
 public List<NoteVM> Notes{get;set;}
}

public NoteVM
{
 public string Subject{get;set;}
 public string Description{get;set;}
} 

I want to do some automapping, and to do that I have to skip the noteContractDM. The following obviously doesnt work:

Mapper.CreateMap<TicketDM, TicketVM>()

I tried something like this:

Mapper.CreateMap<TicketDM, TicketVM>()
  .ForMember(vm => vm.Notes, conf => conf.MapFrom(dm => dm.Notes));  

But it always gives me the Missing type map configuration or unsupported mapping. exception.

1 Answer 1

4

You can tell Automapper to ignore them if you want to skip the object:

Mapper.CreateMap<TicketDM, TicketVM>()
.ForMember(vm => vm.Notes, conf => conf.Ignore());  

But by the example you are giving, you may want to create another map so Notes can also get mapped automatically:

Mapper.CreateMap<TicketDM, TicketVM>()
.ForMember(vm => vm.Notes, conf => conf.MapFrom(dm => dm.Notes.Select(x => x.Note)));

Mapper.CreateMap<NoteDM, NoteVM>();

(example uses System.Linq)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.