.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> 集合中,因为我们最终想要在页面上展示的数据与数据库实体类之间可能 ...
随机推荐
- 修改release management client对应的服务器的地址
参考资料:http://stackoverflow.com/questions/25313053/how-to-change-a-release-management-server-name-in-r ...
- wuzhicms 数据迁移策略
1,本地的域名或ip为特殊域名或ip,勿用 127.0.0.1 或 localhost 或192.168.1.101 等 2,导出数据库,替换所有域名为新域名 3,替换所有文件域名为新域名 4, ...
- ConfigurationManager 缓存刷新
服务没有停止的情况下,如果修改了配置,如果不刷新,是不会生效的,需要在每次重新读取配置前刷新配置文件,具体如下: ConfigurationManager.RefreshSection("a ...
- python3.5 修改 IIS WEB.CONFIG的相关方法
#!/usr/bin/env python3.5 # -*- coding:utf8 -*- from xml.etree.ElementTree import ElementTree,Element ...
- 关于FlagsAttribute
最近在看C#本质论,有介绍FlagsAttribute的特性,看了下源码,发现只是一个简单的特性class和一个构造函数. 调试了一下.NET的源码,发现在console.writeline(***) ...
- 【NOIP2013提高组】货车运输
货车运输 (truck.cpp/c/pas) [问题描述] A国有n座城市,编号从1到n,城市之间有m条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有q辆货车在运输货物,司机们想知道每辆 ...
- nyoj587 hdu1045 简单深搜
#include<iostream> #include<cstdio> #include<queue> #include<vector> #includ ...
- webapi mvc路由注册
在VS.NET 2013中,新建WebAPI项目,代码总的 GlobalConfiguration.Configure(WebApiConfig.Register); 编译时会提示:System.We ...
- 每天点滴的进行,css+div简单布局...布局
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- os库-时间函数
一.时间的三种格式 1.用数值表示时间值 (时间戳)用数字值来表示时间值,实际上时间值的本质就是一个数字值.例如:d = 1131286477 这里的 1131286477 是一个以秒为单位的 格林威 ...