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

  

AutoMapper Profile用法的更多相关文章

  1. [AutoMapper]反射自动注册AutoMapper Profile

    AutoMapper 帮我我们方便管理物件跟物件之间属性值格式转换 模型转换 这里有两个类别 UserInfoModel 当作我们从DB捞取出来模型资料 public class UserInfoMo ...

  2. TransactionScope事务处理方法介绍及.NET Core中的注意事项 SQL Server数据库漏洞评估了解一下 预热ASP.NET MVC 的VIEW [AUTOMAPPER]反射自动注册AUTOMAPPER PROFILE

    TransactionScope事务处理方法介绍及.NET Core中的注意事项   作者:依乐祝 原文链接:https://www.cnblogs.com/yilezhu/p/10170712.ht ...

  3. AutoMapper简单用法

    首先在NuGet添加AutoMapper /// <summary> /// AutoMapper帮助类 /// </summary> public static class ...

  4. Asp.Net AutoMapper用法

    1.AutoMapper简介 用于两个对象映射,例如把Model的属性值赋值给View Model.传统写法会一个一个属性的映射很麻烦,使用AutoMapper两句代码搞定. 2.AutoMapper ...

  5. 【aspnetcore】在asp.net core中配置使用AutoMapper

    网上使用AutoMapper的文章很多,就不多说了.这里主要记录一下怎么在项目中配置和使用. 首先是从NuGet获取AutoMapper. 在Startup.cs文件中注册AutoMapper服务 p ...

  6. ASP.NET Core Web 应用程序系列(五)- 在ASP.NET Core中使用AutoMapper进行实体映射

    本章主要简单介绍下在ASP.NET Core中如何使用AutoMapper进行实体映射.在正式进入主题之前我们来看下几个概念: 1.数据库持久化对象PO(Persistent Object):顾名思义 ...

  7. 【道德经】漫谈实体、对象、DTO及AutoMapper的使用

    写在前面 实体和值对象 实体和对象 故常无欲以观其妙,常有欲以观其徼 初始实体和演化实体 代码中的DTO AutoMapper实体转换 后记 实体(Entity).对象(Object).DTO(Dat ...

  8. 对象映射工具AutoMapper介绍

    AutoMapper是用来解决对象之间映射转换的类库.对于我们开发人员来说,写对象之间互相转换的代码是一件极其浪费生命的事情,AutoMapper能够帮助我们节省不少时间. 一. AutoMapper ...

  9. 转://SQL PROFILE

    我们经常会碰到一些线上的SQL问题,因为执行计划不对,可能需要添加HINT才能解决.但是添加HINT就意味着需要修改应用代码.一般一个应用代码的修改.测试及发布,可能需要两三个工作日才可完成.咱们数据 ...

随机推荐

  1. IPv4 IPv6

    IP提供了一种best-effort,connection-less数据delivery服务. best-efforty意味着它不保证IP数据段会成功到达目的地.当error发生时,IP通常选择丢弃d ...

  2. vue项目 (1) vue结合mini-ui 的使用

    1.安装 npm install mini-ui -S 2. 使用 import Vue from 'vue' import App from './App' import router from ' ...

  3. H5调用百度地图导航

    template <div class="map"> <div class="content_flex"><img src=&qu ...

  4. 利用Delphi全面控制Windows任务栏

    使用Windows95/NT/98操作系统的用户知道:Windows正常启动后,在电脑屏幕下方出现一块 任务栏.从系统功能角度而言,整个任务栏包括几个不同的子区域,从左至右依次是:开始 按钮.应用程序 ...

  5. NX二次开发-UFUN关闭STL文件函数UF_STD_close_stl_file

    NX9+VS2012 #include <uf.h> #include <uf_obj.h> #include <uf_modl.h> #include <u ...

  6. NX二次开发-UFUN获得边的类型UF_MODL_ask_edge_type

    1 NX11+VS2013 2 3 #include <uf.h> 4 #include <uf_ui.h> 5 #include <uf_modl.h> 6 #i ...

  7. iOS 点击Application icon加载推送通知Data

    今天做APNS远程推送通知遇到了一个问题,就是手机接收到通知的时候,如果马上点击通知的 alert view时候,系统马上唤醒你的Application,通知或调用你的didReceiveLocalN ...

  8. prop不同数据类型设置默认值

    vue prop 会接收不同的数据类型,这里列出了 常用的数据类型的设置默认值的写法,其中包含: Number, String, Boolean, Array,  Function, Object   ...

  9. ionic-CSS:ionic Range

    ylbtech-ionic-CSS:ionic Range 1.返回顶部 1. ionic Range ionic Range 是一个滑块控件,ionic 为 Range 提供了很多种默认的样式.而且 ...

  10. Openstack Nova 源码分析 — 使用 VCDriver 创建 VMware Instance

    目录 目录 前言 流程图 nova-compute vCenter 前言 在上一篇Openstack Nova 源码分析 - Create instances (nova-conductor阶段)中, ...