.Net Core 中使用AutoMapper
1、新建一个类
using AutoMapper;
using YourModels;
using YourViewModels;
namespace YourNamespace
{
public class AutoMapperProfileConfiguration : Profile
{
protected override void Configure()
{
CreateMap<Application, ApplicationViewModel>();
CreateMap<ApplicationViewModel, Application>();
...
}
}
}
2、在Startup.cs中增加MapperConfiguration属性
private MapperConfiguration _mapperConfiguration { get; set; }
3、在Startup.cs中的Startup方法中增加
_mapperConfiguration = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new AutoMapperProfileConfiguration());
});
4、在ConfigureServices()中增加
services.AddSingleton<IMapper>(sp => _mapperConfiguration.CreateMapper());
5、使用
using AutoMapper;
using ...
namespace YourNamespace
{
public class ApplicationsController : BaseController
{
[FromServices]
private IMapper _mapper { get; set; }
[FromServices]
private IApplicationRepository _applicationRepository { get; set; }
public ApplicationsController(
IMapper mapper,
IApplicationRepository applicationRepository)
{
_mapper = mapper;
_applicationRepository = applicationRepository;
}
// GET: Applications
public async Task<IActionResult> Index()
{
IEnumerable<Application> applications = await _applicationRepository.GetForIdAsync(...);
if (applications == null)
return HttpNotFound();
List<ApplicationViewModel> viewModel = _mapper.Map<List<ApplicationViewModel>>(applications);
return View(viewModel);
}
...
}
.Net Core 中使用AutoMapper的更多相关文章
- Dotnet Core中使用AutoMapper
官网:http://automapper.org/ 文档:https://automapper.readthedocs.io/en/latest/index.html GitHub:https://g ...
- ASP.NET CORE 中使用AutoMapper进行对象映射
ASP.NET CORE 中使用AutoMapper进行对象映射 1.什么是AutoMapper? AutoMapper是基于对象到对象约定的映射工具,常用于(但并不仅限制于)把复杂的对象模型转为DT ...
- ASP.NET.Core中使用AutoMapper
首先需要在NuGet中引用AutoMapper的类库 install-package AutoMapper install-package AutoMapper.Extensions.Micros ...
- ASP.NET Core Web 应用程序系列(五)- 在ASP.NET Core中使用AutoMapper进行实体映射
本章主要简单介绍下在ASP.NET Core中如何使用AutoMapper进行实体映射.在正式进入主题之前我们来看下几个概念: 1.数据库持久化对象PO(Persistent Object):顾名思义 ...
- .NET Core中使用AutoMapper
何为AutoMapper AutoMapper是对象到对象的映射工具.在完成映射规则之后,AutoMapper可以将源对象转换为目标对象. 安装AutoMapper 这里我们在NuGet中下载安装Au ...
- .NET CORE 中使用AutoMapper进行对象映射
简介 AutoMapper uses a fluent configuration API to define an object-object mapping strategy. AutoMappe ...
- 在 ASP.NET Core 中使用 AutoMapper 使 Entity 和 Resource 之间进行映射
目录 从 NuGet 安装 AutoMapper 添加 Entity类 和 Resource类 添加一个 Profile文件,配置映射关系 在Startup中对AutoMapper进行注册 在项目中使 ...
- TransactionScope事务处理方法介绍及.NET Core中的注意事项 SQL Server数据库漏洞评估了解一下 预热ASP.NET MVC 的VIEW [AUTOMAPPER]反射自动注册AUTOMAPPER PROFILE
TransactionScope事务处理方法介绍及.NET Core中的注意事项 作者:依乐祝 原文链接:https://www.cnblogs.com/yilezhu/p/10170712.ht ...
- 在 ASP.NET Core 项目中使用 AutoMapper 进行实体映射
一.前言 在实际项目开发过程中,我们使用到的各种 ORM 组件都可以很便捷的将我们获取到的数据绑定到对应的 List<T> 集合中,因为我们最终想要在页面上展示的数据与数据库实体类之间可能 ...
随机推荐
- JS 高效快速的数组去重
Array.prototype.uniquer = function() { var result = [], hash = {}; ; i < this.length; i++) { if ( ...
- Let's Encrypt(开源SSL证书管理工具)
刚装上一个StartSSL 的证书没几天,却看到官方发出这样的通知: 无聊中看到了网上各种相关的扯淡: http://tieba.baidu.com/p/4801786642 http://www.t ...
- Unity2D开发小细节
当某个触碰物体挂在父组件时,如果当前子组件不加rigidbody,会默认使用父组件的rigidbody
- flex属性值----弹性盒子布局
里面的属性都 是在有display: flex的情况 下才生效. 兼容性写法: display: -webkit-box; /*老版本语法: Safari, iOS, Android browse ...
- 初始化git
git config --global user.name "Firstname Lastname" git config --global user.email "yo ...
- myeclipse6.0安装svn插件
myeclipse6.0安装svn插件 转载地址:http://www.cnblogs.com/danica/archive/2011/07/12/2104323.html myeclipse6.0安 ...
- Gradle学习草稿
参考博客:http://www.cnblogs.com/davenkin/p/gradle-learning-1.html Android Plugin DSL Reference http://go ...
- 关于用PS改变图像颜色
由于写的一个页面主色调变了,里面的一些图标颜色也要相应改变,自己难得重新去psd里面截图,就想着用ps,看能否直接能变换一下图标颜色.其实方法也很简单的. 1:用ps打开需要改变图标颜色的文件,然后选 ...
- mysql for windows zip版安装
1.将mysql_5.6.24_winx64.zip 解压到文件夹 2.增加环境变量 3.修改mysql配置文件 将mysql根目录下的my-default.ini 复制一份更名为 my.ini.修改 ...
- MEAN全栈开发实践