AutoMapper:映射子集合

时间:2012-12-07 17:00:16

标签: c# automapper

AutoMapper新手问题。

我有一个源和目标DTO,它们具有相同的字段和子集合。 AutoMapper如何映射这些?

简化的源和目标DTO共享相同的名称:

Customer
    Orders
       Invoices
    CustomerInfo

1 个答案:

答案 0 :(得分:10)

Mapper.CreateMap<Customer, CustomerModel>();
Mapper.CreateMap<Orders, OrderModel>();
Mapper.CreateMap<Invoices, InvoicesModel>();
Mapper.CreateMap<CustomerInfo, CustomerInfoModel>();
var mappedModel = Mapper.Map<Customer, CustomerModel>(customer);

这是另一个类似的主题:AutoMapper - mapping child collections in viewmodel