我有以下数据模型:
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}
}
和以下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;}
}
我想做一些自动映射,要做到这一点,我必须跳过noteContractDM。下面的代码显然行不通:
Mapper.CreateMap<TicketDM, TicketVM>()
我试过这样做:
Mapper.CreateMap<TicketDM, TicketVM>()
.ForMember(vm => vm.Notes, conf => conf.MapFrom(dm => dm.Notes));
但是它总是给我Missing type map configuration or unsupported mapping.
异常
如果你想跳过对象,可以告诉Automapper忽略它们:
Mapper.CreateMap<TicketDM, TicketVM>()
.ForMember(vm => vm.Notes, conf => conf.Ignore());
但是根据你给出的例子,你可能想要创建另一个地图,这样Notes
也可以自动映射:
Mapper.CreateMap<TicketDM, TicketVM>()
.ForMember(vm => vm.Notes, conf => conf.MapFrom(dm => dm.Notes.Select(x => x.Note)));
Mapper.CreateMap<NoteDM, NoteVM>();
(示例使用System.Linq)