automapper-2

Automapper projection and union

陌路散爱 提交于 2020-01-07 03:08:17
问题 I have a problem with union and automapper projections. I have two entities: public class Entity { public DateTime ActionDate { get; set; } public int SomeProp { get; set; } } public class ExtendedEntity { public DateTime ActionDate { get; set; } public int SomeProp { get; set; } public int SomeOtherProp { get; set; } } and projection: public class EntityProjection { public DateTime ActionDate { get; set; } public int SomeProp { get; set; } public int SomeOtherProp { get; set; } public string

Automapper copy List to List

…衆ロ難τιáo~ 提交于 2019-12-29 12:13:50
问题 I have these classes : public class Person { public int Id{ get; set ;} public string FirstName{ get; set ;} public string LastName{ get; set ;} } public class PersonView { public int Id{ get; set ;} public string FirstName{ get; set ;} public string LastName{ get; set ;} } I defined this : Mapper.CreateMap<Person, PersonView>(); Mapper.CreateMap<PersonView, Person>() .ForMember(person => person.Id, opt => opt.Ignore()); That's work for this : PersonView personView = Mapper.Map<Person,

Automapper copy List to List

北城以北 提交于 2019-12-29 12:12:30
问题 I have these classes : public class Person { public int Id{ get; set ;} public string FirstName{ get; set ;} public string LastName{ get; set ;} } public class PersonView { public int Id{ get; set ;} public string FirstName{ get; set ;} public string LastName{ get; set ;} } I defined this : Mapper.CreateMap<Person, PersonView>(); Mapper.CreateMap<PersonView, Person>() .ForMember(person => person.Id, opt => opt.Ignore()); That's work for this : PersonView personView = Mapper.Map<Person,

Using AutoMapper to map the property of an object to a string

狂风中的少年 提交于 2019-12-28 05:57:19
问题 I have the following model: public class Tag { public int Id { get; set; } public string Name { get; set; } } I want to be able to use AutoMapper to map the Name property of the Tag type to a string property in one of my viewmodels. I have created a custom resolver to try to handle this mapping, using the following code: public class TagToStringResolver : ValueResolver<Tag, string> { protected override string ResolveCore(Tag source) { return source.Name ?? string.Empty; } } I am mapping using

Using AutoMapper to map the property of an object to a string

浪尽此生 提交于 2019-12-28 05:57:00
问题 I have the following model: public class Tag { public int Id { get; set; } public string Name { get; set; } } I want to be able to use AutoMapper to map the Name property of the Tag type to a string property in one of my viewmodels. I have created a custom resolver to try to handle this mapping, using the following code: public class TagToStringResolver : ValueResolver<Tag, string> { protected override string ResolveCore(Tag source) { return source.Name ?? string.Empty; } } I am mapping using

AutoMapper using the wrong constructor

天大地大妈咪最大 提交于 2019-12-21 03:12:40
问题 Today I upgraded a fully functioning application using AutoMapper v1.1 to now use AutoMapper v2.1 and I am coming across some issues that I never encountered using the previous version. Here is an example of my code mapping back from Dto to Domain object public class TypeOne { public TypeOne() { } public TypeOne(TypeTwo two) { //throw ex if two is null } public TypeOne(TypeTwo two, TypeThree three) { //throw ex if two or three are null } public TypeTwo Two {get; private set;} public TypeThree

AutoMapper Custom Type Converter not working

主宰稳场 提交于 2019-12-18 11:33:20
问题 I am using Troy Goode's PagedList to provide paging information in my WebApi. His package returns an IPagedList that implements IEnumerable but also contains custom properties such as IsLastPage, PageNumber, PageCount, etc. When you try to return this class from a WebApi controller method (such as the GET), the Enumerable is serialized, but the custom properties are not. So, I thought I would use AutoMapper and write a custom type converter to convert to a class such as this: public class

AutoMapper and flattening nested arrays

妖精的绣舞 提交于 2019-12-17 18:29:50
问题 I'm trying to use AutoMapper to flatten multiple levels of arrays. Consider the following source classes: class X { public string A { get; set; } public Y[] B { get; set; } } class Y { public string C { get; set; } public Z[] D { get; set; } } class Z { public string E { get; set; } public string F { get; set; } } And the following destination: class Destination { public string A { get; set; } public string C { get; set; } public string E { get; set; } public string F { get; set; } } What I'd

AutoMapper: What is the difference between MapFrom and ResolveUsing?

北城以北 提交于 2019-12-17 07:26:55
问题 Ignoring the ResolveUsing overloads that take an IValueResolver, and looking only at these 2 methods: void ResolveUsing(Func<TSource, object> resolver); void MapFrom<TMember>(Expression<Func<TSource, TMember>> sourceMember); The main difference between these 2 seems to be that ResolveUsing takes a Func<TSource, object> , whereas MapFrom takes an Expression<Func<TSource, TMember>> . However in client code that actually uses one of these methods with a lambda expression, they seem to be

Map list to existing list in Automapper using a key

眉间皱痕 提交于 2019-12-10 15:04:13
问题 Automapper easily handles mapping one list of object types to another list of different objects types, but is it possible to have it map to an existing list using an ID as a key? 回答1: I have not found better way than the following. Here are source and destination. public class Source { public int Id { get; set; } public string Foo { get; set; } } public class Destination { public int Id { get; set; } public string Foo { get; set; } } Define converter (You should change List<> to whatever type