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. 在GitHub上编辑README.md排版样式

    如何在github的readme.md编辑出好看的样式排版文章呢...? 参考:https://blog.csdn.net/u012067966/article/details/50736647

  2. Batch - FINDSTR

    总结 Searches for strings in files. 在文件中寻找特定的字符串 官方文档 C:\Users\cuixunxu>FINDSTR /? Searches for str ...

  3. 如何配置vue-cli4.0

    这是一期主要分享vue-cli4.0配置 新建一个项目,最令人为难的是配置环境.拿vue来说,创建项目很简单,跟着文档走即可,但是要知道配置本地,测试,生产环境,以及反向代理等等,如果对于一个对vue ...

  4. python编写微信公众号首图思路详解

    前言 之前一直在美图秀秀调整自己的微信公众号首图,效果也不尽如人意,老是调来调去,最后发出来的图片被裁剪了一大部分,丢失部分关键信息,十分恼火,于是想着用python写一个程序,把微信公众号首图的模式 ...

  5. git Web

    { …or create a new repository on the command line   echo "# Kotlin" >> README.md git ...

  6. 牛客多校第三次B——线段树维护线性基交

    写线性基交函数时调试了半天.. #include<bits/stdc++.h> using namespace std; #define ll long long #define maxn ...

  7. delphi里为程序任务栏右键菜单添加自定义菜单

    本文讲解的是为自身程序的任务栏右键菜单里添加自己定义的菜单的方法: delphi添加任务栏右键菜单 procedure TForm1.FormCreate(Sender: TObject); var ...

  8. Delphi窗体重绘API

    WinAPI: DrawFocusRect - 绘制焦点矩形 用SetTextColor()设置颜色 功能 设置指定设备环境(HDC)的字体颜色原型 WINGDIAPI COLORREF WINAPI ...

  9. NOIp2018集训test-9-2(am)

    一场讲述谁比谁更傻逼的普及组比赛,证明了 1.老张是魔鬼(为什么有这么多套普及组题??) 2.我最傻逼 第 1 题 谜题 判断1~99哪些数翻转后合法,找到最长的连续合法段的长度,为4,所以n< ...

  10. NX二次开发-UFUN创建块UF_MODL_create_block

    NX9+VS2012 #include <uf.h> #include <uf_modl.h> UF_initialize(); UF_FEATURE_SIGN Sign = ...