automapper

Automapper, Mapping one object member type to multiple concrete type

血红的双手。 提交于 2019-12-13 02:04:26
问题 I have this Party class which contains an object data type coming from a service. It can contain two different member types for the Item property. public class Party { public string DMVID {get; set;} public object Item { get; set; } } and this DTO public class PartyDTO { public string DMVID {get; set;} public BusinessDTO BusinessItem { get; set; } public IndividualDTO IndividualItem { get; set; } } How can I map the output of the Item to BusinessItem or IndividualItem . I know this one would

Non-static AutoMapper and ASP.NET MVC

蓝咒 提交于 2019-12-13 01:45:09
问题 Similarly to this question: Where to place AutoMapper.CreateMaps? Where is the recommended place to put the non-static AutoMapper initialization? var map = new MapperConfiguration( cfg => ... ).CreateMapper(); Where is the recommended place to store the map variable in order for it to be accessible from controllers? Thanks in advance. 回答1: A good approach to this is to use dependency injection and inject the mapper on your components that need access to it. This new approach to AutoMapper is

use automapper to map single object into a list of objects

眉间皱痕 提交于 2019-12-13 00:22:35
问题 I have the following line in my mapper: I am trying to map from one model where I have a single property called Result to a model where I have a List of Results. I have the following so far: options.CreateMap<Contract.Dto.Result, List<Result>>(MemberList.Source).ConvertUsing<ResultConverter>(); internal class ResultConverter : ITypeConverter<Contract.Dto.Result, List<Result>> { public List<Result> Convert(Contract.Dto.Result source, List<Result> destination, ResolutionContext context) {

AutoMapper don't work with entity EF Core

落爺英雄遲暮 提交于 2019-12-12 23:38:50
问题 I'm trying to get view from entity with fields that I specify at expandProperties with .ProjectTo(). Using .Net Core 2 + Ef Core + AutoMapper There is entities: public class LessonCatalog { public string Name { get; set; } public int? ImageId { get; set; } public virtual Image Image { get; set; } public virtual ICollection<Lesson> Lessons { get; set; } } public class Lesson { public string Name { get; set; } public string Description { get; set; } public int? ImageId { get; set; } public

Automapper missing type map configuration or unsupported mapping

时光总嘲笑我的痴心妄想 提交于 2019-12-12 21:25:04
问题 I have 2 class : Class 1 : (Domain) public class Book { public ObjectId Id { get; set; } public String ISBN { get; set; } public String Title { get; set; } public String Publisher { get; set; } public int? PageCount { get; set; } public Author Author { get; set; } } Class 2 : (Repository) public class Book { public ObjectId Id { get; set; } public String ISBN { get; set; } public String Title { get; set; } public String Publisher { get; set; } [BsonIgnoreIfNull] public int? PageCount { get;

How to Define Extension Method for ICollection<T> where T : IMyInterface without Specifying T in the Method Definition

痴心易碎 提交于 2019-12-12 21:04:37
问题 Background: I want to hook in business rules at the point that DTOs are being mapped to entities. I figured encapsulating the mapping into an extension method would be a good route. IEntityDto is the interface that all DTOs that can be directly mapped to entities implement. The single instance works fine: public static TEntity MapTo<TEntity>(this IEntityDto dto) { ... Run Business Rules that don't require db access ... return AutoMapper.Mapper.Map<TEntity>(dto); } I'd like to also extend

AutoMapper with auto-incremented values

折月煮酒 提交于 2019-12-12 20:19:03
问题 public class OrderDTO { public string ClientName { get; set; } public ICollection<OrderDetailDTO> Details { get; set; } } public class Order { public string ClientName { get; set; } public ICollection<OrderDetail> Details { get; set; } } public class OrderDetailDTO { public int Quantity { get; set; } public string ProductName { get; set; } } public class OrderDetail { public int OrderId { get; set; } public int Quantity { get; set; } public string ProductName { get; set; } } Let's say there

How not to lose properties that are present in my destination object but are NOT in my source object after mapping using AutoMapper

馋奶兔 提交于 2019-12-12 18:28:37
问题 I have a following class structure: public class A { public bool Property1 { get; set; } public bool Property2 { get; set; } } public class ContainerForA { public A A { get; set; } } public class A1 { public bool Property1 { get; set; } } public class ContainerForA1 { public A1 A { get; set; } } I create a mapping for this set of classes: Mapper.CreateMap<A1, A>(); Mapper.CreateMap<ContainerForA1, ContainerForA>(); I create an instance of this set of classes: var cnt_a = new ContainerForA() {

Ninject MVC 2, EF 4, AutoMapper - ObjectContext has been disposed

偶尔善良 提交于 2019-12-12 18:28:29
问题 Completely revised: Okay, I'm using Ninject with the MVC 2 extension as my DI container, and AutoMapper as my entity <--> view model mapper. I'm getting an 'ObjectContext is disposed' error in my view model -> entity mapping. My code is below. Ninject bindings: public class DIModule : NinjectModule { public override void Load() { this.Bind<HGEntities>().ToSelf().InRequestScope(); this.Bind<IArticleRepository>().To<HGArticleRepository>().InRequestScope(); this.Bind<IGameRepository>().To

Using AutoMapper to dynamically map objects including arrays

核能气质少年 提交于 2019-12-12 18:25:12
问题 I'm trying to build a way of mapping from one type to another, knwoing they will (should) have the same structure. Related Question. For ease of the fiddly bits, I'm using AutoMapper from Codeplex, with the following function: private static List<Type> seenTypes = new List<Type>(); private static void MapDataObjects(Type a, Type b) { AutoMapper.Mapper.CreateMap(a, b); PropertyInfo[] aProps = a.GetProperties(); PropertyInfo[] bProps = b.GetProperties(); foreach (PropertyInfo aProp in aProps) {