automapper

How to configure AutoMapper for Polymorphism with explicit member mapping?

半城伤御伤魂 提交于 2019-12-01 05:35:58
Consider the following basic case: Mapper.CreateMap<FromBase, ToBase>() .Include<FromD1, ToD1>() .Include<FromD2, ToD2>(); Mapper.CreateMap<FromD1, ToD1>() .ForMember( m => m.P0, a => a.MapFrom( x => x.Prop0 ) ) .ForMember( m => m.P1, a => a.MapFrom( x => x.Prop1 ) ); Mapper.CreateMap<FromD2, ToD2>() .ForMember( m => m.P0, a => a.MapFrom( x => x.Prop0 ) ) .ForMember( m => m.P2, a => a.MapFrom( x => x.Prop2 ) ); Mapper.AssertConfigurationIsValid(); FromBase[] froms = { new FromD1() { Prop0 = 10, Prop1 = 11 }, new FromD2() { Prop0 = 20, Prop2 = 22 } }; var tos = Mapper.Map<FromBase[], ToBase[]>(

best way to project ViewModel back into Model

扶醉桌前 提交于 2019-12-01 04:42:28
Consider having ViewModel : public class ViewModel { public int id {get;set;} public int a {get;set;} public int b {get;set;} } and a original Model like this: public class Model { public int id {get;set;} public int a {get;set;} public int b {get;set;} public int c {get;set;} public virtual Object d {get;set;} } Each time I get view model I have to put all ViewModel properties one by one into Model. Something like : var model = Db.Models.Find(viewModel.Id); model.a = viewModel.a; model.b = viewModel.b; Db.SaveChanges(); Which always cause lots of problem. I even sometimes forget to mention

automapper - ignore mapping if property type is different with same property name - C#

浪尽此生 提交于 2019-12-01 04:11:39
How can I ignore mapping if property type is different with same property name? By default it's throwing error. Mapper.CreateMap<EntityAttribute, LeadManagementService.LeadEntityAttribute>(); Model = Mapper.Map<EntityAttribute, LeadManagementService.LeadEntityAttribute>(EntityAttribute); I know a way to specify the property name to ignore but that's not what I want. .ForMember(d=>d.Field, m=>m.Ignore()); Because in the future I might add new properties. So i need to ignore mapping for all properties with different data types. You should use ignore for properties that should be ignored: Mapper

Using AutoMapper to map unknown types

眉间皱痕 提交于 2019-12-01 03:17:27
I'm using AutoMapper to copy the properties of one object to another: This is my code: // Get type and create first object Type itemType = Type.GetType(itemTypeName); var item = Activator.CreateInstance(itemType); // Set item properties .. Code removed for clarity .. // Get item from Entity Framework DbContext var set = dataContext.Set(itemType); var itemInDatabase = set.Find(id); if (itemInDatabase == null) { itemInDatabase = Activator.CreateInstance(itemType); set.Add(itemInDatabase); } // Copy item to itemInDatabase Mapper.CreateMap(itemType, itemType); Mapper.Map(item, itemInDatabase); //

Generic extension method for automapper

强颜欢笑 提交于 2019-12-01 03:10:24
问题 public abstract class Entity : IEntity { [Key] public virtual int Id { get; set; } } public class City:Entity { public string Code { get; set; } } public class BaseViewModel:IBaseViewModel { public int Id { get; set; } } public class CityModel:BaseViewModel { public string Code { get; set; } } my domain and view classes... and for mapping extension public static TModel ToModel<TModel,TEntity>(this TEntity entity) where TModel:IBaseViewModel where TEntity:IEntity { return Mapper.Map<TEntity,

AutoMapper - why use Map over DynamicMap?

不打扰是莪最后的温柔 提交于 2019-12-01 02:40:33
Assuming the objects you're mapping with AutoMapper require no custom mappings, is there ever a point in doing this: Mapper.CreateMap<Src, Dest>(); // .... Mapper.Map(SrcObject, DestObj); If no custom mappings are required, does the above approach gain you anything over just using DynamicMap without the need for any prior configuration? Mapper.DynamicMap(SrcObject, DestObj); I do understand that DynamicMap is required when you're mapping anonymous types, but I'm asking about whether DyanmicMap is ever not preferred for static types that require no custom mappings. Been a while since I last

AutoMapper

别等时光非礼了梦想. 提交于 2019-12-01 02:36:37
扩展方法 /// <summary> /// 类型映射,默认字段名字一一对应 /// </summary> /// <typeparam name="TDestination">转化之后的model,可以理解为viewmodel</typeparam> /// <typeparam name="TSource">要被转化的实体,Entity</typeparam> /// <param name="source">可以使用这个扩展方法的类型,任何引用类型</param> /// <returns>转化之后的实体</returns> public static TDestination MapTo<TDestination, TSource>(this TSource source) where TDestination : class where TSource : class { if (source == null) return default(TDestination); var config = new MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>()); var mapper = config.CreateMapper(); return mapper.Map<TDestination>

在 ASP.NET Core 项目中使用 AutoMapper 进行实体映射

微笑、不失礼 提交于 2019-12-01 00:12:48
原文: 在 ASP.NET Core 项目中使用 AutoMapper 进行实体映射 一、前言 #   在实际项目开发过程中,我们使用到的各种 ORM 组件都可以很便捷的将我们获取到的数据绑定到对应的 List<T> 集合中,因为我们最终想要在页面上展示的数据与数据库实体类之间可能存在很大的差异,所以这里更常见的方法是去创建一些对应于页面数据展示的 `视图模型` 类,通过对获取到的数据进行二次加工,从而满足实际页面显示的需要。   因此,如何更便捷的去实现 数据库持久化对象 与 视图对象 间的实体映射,避免我们在代码中去一次次的手工实现这一过程,就可以降低开发的工作量,而 AutoMapper 则是可以帮助我们便捷的实现实体转换这一过程的利器。所以,本章我们就来学习如何在 ASP.NET Core 项目中通过使用 AutoMapper 去完成实体间的映射。   当然,如果你习惯于从视图展现到持久化到数据库都采用数据库实体,那么本篇文章对你可能不会有任何的帮助。   代码仓储: https://github.com/Lanesra712/grapefruit-common/tree/master/sample/aspnetcore/aspnetcore-automapper-tutorial 二、Step by Step #   AutoMapper 是一个 OOM(Object

AutoMapper - why use Map over DynamicMap?

ⅰ亾dé卋堺 提交于 2019-11-30 22:26:23
问题 Assuming the objects you're mapping with AutoMapper require no custom mappings, is there ever a point in doing this: Mapper.CreateMap<Src, Dest>(); // .... Mapper.Map(SrcObject, DestObj); If no custom mappings are required, does the above approach gain you anything over just using DynamicMap without the need for any prior configuration? Mapper.DynamicMap(SrcObject, DestObj); I do understand that DynamicMap is required when you're mapping anonymous types, but I'm asking about whether

How do I get AutoMapper to deal with a custom naming convention?

让人想犯罪 __ 提交于 2019-11-30 21:28:23
In the project I'm working on, we are mapping auto-generated DTOs to business objects. The database has an ahem unusual (but largely consistent) naming convention, which means that it's possible to transform most DTO property names to their equivalent business object property names, thus saving many lines of code. For example, in the DTO (and database) we have a property called account_ID__created that will map to a BO property called CreatedAccountId . This is the kind of transformation happening in MemberNameTransformer.GetBoMemberName() , so it's not as simple as a slightly different