1.add service to provider (need nuget a package before add this service)
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
2.create a mapperProFile(this class need inherit Profile)
public class CompanyProfile:Profile
{
//映射的配置文件
public CompanyProfile()
{
//从Company映射到CompanyDto
//约定:1.原属性和目标属性一样的话,他属性的值就会赋给目标属性
//2.如果目标的属性再原属性中不存在,直接就忽略了,不会对它进行赋值
CreateMap<Company, CompanyDto>()
//执行手动映射(常用)
.ForMember(
//Name 映射到 CompanyName
memberOptions: opt => opt.MapFrom(mapExpression: src => src.Name),
destinationMember: dest => dest.CompanyName
);
CreateMap<CompanyAddDto, Company>();//属性名称一样,这样写就可以了
}
}
3.use mapper
var companyDto = _mapper.Map
也可以将集合映射成另一个集合
_mapper.Map<IEnumerable