我有以下数据模型:
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}
}
以及以下 ViewModel:
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());
但根据您给出的示例,您可能想要创建另一个 map ,以便 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)
