- 在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()));
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);
}