using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoMapper; namespace MvcAutoMapper.AutoMapper
{
public class Configuration
{
public static void Configure()
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile<UserProfile>();
});
}
}
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoMapper; namespace MvcAutoMapper.AutoMapper.Profiles
{
public class UserProfile:Profile
{ protected override void Configure()
{
CreateMap<Models.User, Models.UserDto>(); }
}
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoMapper; namespace MyMvc.AutoMapper
{
public static class AutoMapperForMvc
{
public static void Register()
{
Mapper.Initialize(x =>
{
x.AddProfile<UserProfile>();
}); //在程序启动时对所有的配置进行严格验证
Mapper.AssertConfigurationIsValid();
}
}
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoMapper; namespace MyMvc.AutoMapper
{
public class UserProfile:Profile
{
public UserProfile()
{
base.CreateMap<Models.User, Models.UserView>()
//映射前
.BeforeMap((s, t) =>
{
s.Age += 10;
})
//映射后
.AfterMap((s, t) =>
{
t.Age += 10;
})
//条件判断
//年龄不大于10 不映射年龄这个属性的值,那值就是默认的0,但是映射后+10,所以就是10
.ForMember(t=>t.Age,o=>o.Condition(s=>s.Age>30))
//空值
.ForMember(t=>t.Name, o=>o.NullSubstitute("无名氏"))
.ForMember(x=>x.time,x=>x.MapFrom(s=>s.add_time))
//反向映射
.ReverseMap();
}
}
}

  

最新文章

  1. arm,iptables: No chain/target/match by that name.
  2. 转:在VS2010下编译、调试和生成mex文件
  3. Weblogic监控指标
  4. Android 摇一摇 之 震动片
  5. JS学习笔记10_Ajax
  6. 转:SDL2源代码分析
  7. Ubuntu下配置C/C++开发环境
  8. 设计模式之 -- 单例模式(Singleton)
  9. maven的中传递依赖,maven的依赖管理(转)
  10. 【LEETCODE OJ】Binary Tree Preorder Traversal
  11. C# 必应代码搜索
  12. [C++]对象的销毁机制
  13. Struts2配置RESULT中TYPE的参数说明
  14. 红豆带你从零学C#系列之:开始C#编程(一)
  15. 实战!基于lamp安装Discuz论坛-技术流ken
  16. fastJson 之 JSONPath使用
  17. 集群LVS
  18. hook mono实现Assembly.Load从指定路径读取文件
  19. Beta任务项录入
  20. solr学习四(关于性能的杂知识)

热门文章

  1. 笔记55 Mybatis快速入门(六)
  2. systemctl命令的使用及服务状态的查看
  3. 解决在移动端上 click事件延迟300 毫秒的问题 fastclick.js
  4. Android SDK Download
  5. Springboot文件上传限制
  6. 利用Delphi全面控制Windows任务栏
  7. 如何将Canvas中内容保存为图片
  8. JDK 安装步骤
  9. DLL劫持技术例子: HijackDll
  10. 事件绑定addEventListener