.netcore3.1 添加AutoMapper自动映射

 

  • 在Nuget包管理中,搜索AutoMapper,添加引用

  • 创建配置属性管理类
   public class AutoMapperProfile : AutoMapper.Profile
    {
        public AutoMapperProfile()
        {
            //实体字段一致情况 <TSource, TDestination>
            CreateMap<SelfRequest,entity_self > ();
        }
    }
  • 在Startup.cs中的Service中注入服务
  //automapper
             services.AddAutoMapper(c=>c.AddProfile(new AutoMapperProfile()));
  • 在controller类中,使用
        private readonly IMapper _mapper;
     
        public SelfController(ILogger<SelfController> logger, IMapper mapper)
        {
            _logger = logger;
         
            _mapper = mapper;
        }
  [HttpPost("Save")]
        public async Task<WebApiResult> Save([FromBody] SelfRequest request)
        {
            var enity = _mapper.Map<entity_self>(request);
      
           
        }

 

posted @ 2021-06-10 13:58  低调码农哥!  阅读(185)  评论(0编辑  收藏  举报