-
C#教程之C#教程之简单的C#实体映射 AutoMapper
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
AutoMapper是对象到对象的映射工具。在完成映射规则之后,AutoMapper可以将源对象转换为目标对象。
要映射实体
1 public class SourceModel 2 { 3 public int ID { get; set; } 4 public string Name { get; set; } 5 public string Address { get; set; } 6 public string Mobile { get; set; } 7 }
被映射实体
1 public class YingSheModel 2 { 3 public string Name { get; set; } 4 public string Address { get; set; } 5 }
需要将SourceModel类的对象映射到YingSheModel类的对象上面。需要对AutoMapper进行如下配置:
//注:Mapper.CreateMap由于nuget的最新版本用法改变了无法使用
Mapper.Initialize(cret => cret.CreateMap<SourceModel, YingSheModel>())
效果展示:
全部代码:
using AutoMapper; using System; namespace Mapping { class Program { static void Main(string[] args) { Mapper.Initialize(cret => cret.CreateMap<SourceModel, YingSheModel>());//配置 SourceModel sources = new SourceModel() { ID = 1, Name = "特朗普", Address = "北京市洪山区", Mobile = "18712457845" }; //给实体赋初始数据 YingSheModel dest = Mapper.Map<YingSheModel>(sources);//看这里的断点 var model = new { name = dest.Name, address = dest.Address }; Console.WriteLine(model); Console.ReadKey(); }
} public class SourceModel { public int ID { get; set; } public string Name { get; set; } public string Address { get; set; } public string Mobile { get; set; } } public class YingSheModel { public string Name { get; set; } public string Address { get; set; } } }
栏目列表
最新更新
如何使用OS模块中的stat方法
Python os 模块
seek() 方法
python打开文件实例1
Python写入文件
什么是流?
文件操作如何进制逐行读取
Python相对路径
with创建临时运行环境
Python文件操作
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
access教程之Access简介
mysql 安装了最新版本8.x版本后的报错:
Mysql空间数据&空间索引(spatial)
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
数据定义功能
数据操作功能