C# 实体转换 AutoMapper

 

//首先 安装 AutoMapper,然后在应用程序启动时配置AutoMapper以了解要映射的类型:

var config = new MapperConfiguration(cfg => { cfg.CreateMap<AreaManagerClassifyEntity, AreaManagerClassifyListEntity>(); });

//然后在您的应用程序代码中,执行映射:
IMapper iMapper = config.CreateMapper();
var classifyResult = iMapper.Map<源实体Entity,目标Entity>(item);

 

1.NuGet安装AutoMapper.Extensions.Microsoft.DependencyInjection

2.创建配置文件,并添加映射配置
需要继承AutoMapper中的Profile

public class AutoMapperProfiles : Profile
{
    public AutoMapperProfiles()
    {
        //构造函数中创建映射关系
        CreateMap<UserCreateInput, User>();
    }
}

 

3.在Startup启动类中的ConfigureServices方法中将服务添加到容器

AutoMapperProfiles是上面步骤中定义的配置文件

services.AddAutoMapper(typeof(AutoMapperProfiles));

4.在当前要使用的地方,构造函数引入一下(IMapper)
5.Mapper.Map<你想要转换成的数据>(数据源);

 

参照:https://blog.csdn.net/sinat_16998945/article/details/103072259

posted @   丁焕轩  阅读(644)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2021-04-29 sql server 一列的多行内容拼接成一行中的一列
点击右上角即可分享
微信分享提示