.NET Core Automapper 指定自定义映射规则的方法
时间:2022-08-14
1、示例代码
下面是实体类Employee和DTO(数据传输对象)类EmployeeDto
public >Employee
{
public long Id {get;set;}
public string Name {get;set;}
public string Phone {get;set;}
public string Fax {get;set;}
public DateTime DateOfBirth {get;set;}
}
public >EmployeeDto
{
public long Id {get;set;}
public string FullName {get;set;}
public DateTime DateOfBirth {get;set;}
}
注意:Employee类的Name和EmployeeDto类FullName属性名字是不同的。
2、Automapper 自定义映射规则(属性对应关系)
针对Employee类的Name和EmployeeDto类FullName属性指定自定义映射规则。
Mapper.CreateMap<Employee, EmployeeDto>()
.ForMember(dest => dest.FullName, opt => opt.MapFrom(src => src.Name));
相关文档:ASP.NET Core Automapper的Nuget安装和配置以及demo示例代码
特别声明:本站部分内容收集于互联网是出于更直观传递信息的目的。该内容版权归原作者所有,并不代表本站赞同其观点和对其真实性负责。如该内容涉及任何第三方合法权利,请及时与824310991@qq.com联系,我们会及时反馈并处理完毕。