使用
Nuget安装
AutoMapper AutoMapper.Extensions.Microsoft.DependencyInjection //依赖注入AutoMapper,需要下载该包。
在Startup中添加AutoMapper
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
//添加对AutoMapper的支持
services.AddAutoMapper();
}创建AutoMapper映射规则
public class AutoMapperConfigs:Profile
{
//添加你的实体映射关系.
public AutoMapperConfigs()
{
CreateMap();
CreateMap();
}
} 在构造函数中注入你的IMapper
IMapper _mapper;
public PoundListController(IMapper mapper)
{
_mapper = mapper;
}单个对象转换
//typeof(model)="PoundSheetViewModel" DBPoundSheet dBPoundSheet = _mapper.Map(model);
集合对象转换