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
分类:
.net core c#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2021-04-29 sql server 一列的多行内容拼接成一行中的一列