[英]How to map properties of another class into parent class using automapper?
I am newbie for Automapper. 我是Automapper的新手。
I have a domin and ViewModel class. 我有一个domin和ViewModel类。
I want to map the domain class to ViewModel class. 我想将域类映射到ViewModel类。
Domain Class : 域类:
public class PurchaseOrder
{
public int Id { get; set; }
public string PONo { get; set; }
public DateTime PODate { get; set; }
public int CompanyId { get; set; }
}
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
}
View Model Class 查看模型类别
public class PurchaseOrderVM
{
public int Id { get; set; }
public string PONo { get; set; }
public DateTime PODate { get; set; }
public int CompanyId { get; set; }
public string CompanyName { get; set; }
}
public class CompanyVM
{
public int Id { get; set; }
public string Name { get; set; }
}
Now, If you can see , I can map ID,PONo,PoDate perfectly. 现在,如果可以看到,我可以完美地映射ID,PONo,PoDate。
But, I want to know how can I map the CompanyID and ComapanyName from Domain to VM class ? 但是,我想知道如何将Domain的CompanyID和ComapanyName映射到VM类?
Transform Method 转换方法
static void POTransform()
{
PurchaseOrder PODomain = new PurchaseOrder();
PODomain.Id = 1;
PODomain.PONo = "154";
PODomain.PODate = Convert.ToDateTime("5-Jun-2014");
Mapper.CreateMap<PurchaseOrder, PurchaseOrderVM>()
.ForMember(dest => dest.CompanyName, Source => Source.MapFrom(????);
}
Mapper.CreateMap<PurchaseOrder, PurchaseOrderVM>()
.ForMember(dest => dest.CompanyName, opt => opt.MapFrom(src => GetCompanyById(src.CompanyId).Name));
}
private Company GetCompanyById(int companyId)
{
....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.