AutoMapper Profile用法
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用法的更多相关文章
- [AutoMapper]反射自动注册AutoMapper Profile
AutoMapper 帮我我们方便管理物件跟物件之间属性值格式转换 模型转换 这里有两个类别 UserInfoModel 当作我们从DB捞取出来模型资料 public class UserInfoMo ...
- TransactionScope事务处理方法介绍及.NET Core中的注意事项 SQL Server数据库漏洞评估了解一下 预热ASP.NET MVC 的VIEW [AUTOMAPPER]反射自动注册AUTOMAPPER PROFILE
TransactionScope事务处理方法介绍及.NET Core中的注意事项 作者:依乐祝 原文链接:https://www.cnblogs.com/yilezhu/p/10170712.ht ...
- AutoMapper简单用法
首先在NuGet添加AutoMapper /// <summary> /// AutoMapper帮助类 /// </summary> public static class ...
- Asp.Net AutoMapper用法
1.AutoMapper简介 用于两个对象映射,例如把Model的属性值赋值给View Model.传统写法会一个一个属性的映射很麻烦,使用AutoMapper两句代码搞定. 2.AutoMapper ...
- 【aspnetcore】在asp.net core中配置使用AutoMapper
网上使用AutoMapper的文章很多,就不多说了.这里主要记录一下怎么在项目中配置和使用. 首先是从NuGet获取AutoMapper. 在Startup.cs文件中注册AutoMapper服务 p ...
- ASP.NET Core Web 应用程序系列(五)- 在ASP.NET Core中使用AutoMapper进行实体映射
本章主要简单介绍下在ASP.NET Core中如何使用AutoMapper进行实体映射.在正式进入主题之前我们来看下几个概念: 1.数据库持久化对象PO(Persistent Object):顾名思义 ...
- 【道德经】漫谈实体、对象、DTO及AutoMapper的使用
写在前面 实体和值对象 实体和对象 故常无欲以观其妙,常有欲以观其徼 初始实体和演化实体 代码中的DTO AutoMapper实体转换 后记 实体(Entity).对象(Object).DTO(Dat ...
- 对象映射工具AutoMapper介绍
AutoMapper是用来解决对象之间映射转换的类库.对于我们开发人员来说,写对象之间互相转换的代码是一件极其浪费生命的事情,AutoMapper能够帮助我们节省不少时间. 一. AutoMapper ...
- 转://SQL PROFILE
我们经常会碰到一些线上的SQL问题,因为执行计划不对,可能需要添加HINT才能解决.但是添加HINT就意味着需要修改应用代码.一般一个应用代码的修改.测试及发布,可能需要两三个工作日才可完成.咱们数据 ...
随机推荐
- Excel如何快速渲染百万级别的数据
Excel主要经历1.查询2.渲染的方式 关于查询: 不同技术水平的人有不同的解决方案,目前我采用的是 1:多线程查询 2:一个异步后台线程每次查询100条便渲染,采用的“懒加载方式”,这样可以做到实 ...
- Spring中使用到的设计模式
1.工厂模式:Beanfactory和ApplicationContext 2.单例模式:bean的构建 3.代理模式:AOP 4.模板模式:jdbcTemplate,hibernateTemplat ...
- Java——面向对象的特征三:多态性
2.5面向对象的特征三:多态性 2.5.1 关于java语言中的向上转型和向下转型 ①向上转型(upcasting) : 子--->父(自动类型转换) ②向下转型(downcasting) : ...
- 代码格式化工具---prettier配置
我自己的常用 prettier 配置如下: // .prettierrc 文件 // 这里修改的都是与默认值不同的,没有修改到的就是启用默认值 // .prettierrc 文件是使用 json 格式 ...
- NX二次开发-UFUN CSYS坐标系转换UF_CSYS_map_point
1 NX9+VS2012 2 3 #include <uf.h> 4 #include <uf_curve.h> 5 #include <uf_csys.h> 6 ...
- flutter WebView flutter js 互调
通过webview 进行flutter 和js 的互调,页面可以放在其他网站 也可以放在本地,我这里放在本地进行测试. 1.引入webview_flutter: ^0.3.5+2 这个是官网推荐用的, ...
- Java-Class-C:com.alibaba.fastjosn.JSON
ylbtech-Java-Class-C:com.alibaba.fastjosn.JSON 1.返回顶部 1.1.import com.alibaba.fastjson.JSON;import co ...
- access数据库调用
1.工程环境配置(vs+qt) 点击项目,右键,选择项目设置,选择SQL 2.获取自己数据库的驱动版本有哪些,如下代码所示: QStringList drivers = QSqlDatabase::d ...
- solr 启动报错Cannot load analyzer: org.wltea.analyzer.lucene.IKAnalyzer
schema.xml 配置文件信息: <field name="title" type="text_ik" indexed="true" ...
- 面向对象编程思想(OOP)总结
本文我将从面向对象编程思想是如何解决软件开发中各种疑难问题的角度,来讲述我们面向对象编程思想的理解,梳理面向对象四大基本特性.七大设计原则和23种设计模式之间的关系. 软件开发中疑难问题: 软件复杂庞 ...