官方文档:http://docs.automapper.org/en/stable/index.html
一、安装和配置:
二、使用:
1、建立 Profile文件:
public class MappingProfile:Profile { public MappingProfile() { CreateMap<Post, PostDTO>().ForMember(dest=>dest.Updatetime,opt=>opt.MapFrom(src=>src.LastModified));//字段映射 CreateMap<PostDTO, Post>(); } }
2、controller 使用
private readonly IMapper _mapper; public PostController(IPostRepository postRepository, IUnitOfWork unitOfWork,IMapper mapper) { _postRepository = postRepository; _unitOfWork = unitOfWork; _mapper = mapper; } [HttpGet] public async Task<IActionResult> Get() { var posts = await _postRepository.GetPosts(); var postDto=_mapper.Map<IEnumerable<Post>,IEnumerable<PostDTO>>(posts); return Ok(postDto); }