简单的C#实体映射 AutoMapper

匿名 (未验证) 提交于 2019-12-02 22:06:11

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     }
View Code
被映射实体
1  public class YingSheModel 2     { 3         public string Name { get; set; } 4         public string Address { get; set; } 5     }
View Code

需要将SourceModel类的对象映射到YingSheModel类的对象上面。需要对AutoMapper进行如下配置:

//注:Mapper.CreateMap由于nuget的最新版本用法改变了无法使用
Mapper.Initialize(cret => cret.CreateMap<SourceModel, YingSheModel>())

效果展示:

全部代码:

 
    }     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; }     } }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!