automapper-2

AutoMapper Custom Type Converter not working

我是研究僧i 提交于 2019-12-02 21:07:21
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 PagedViewModel<T> { public int FirstItemOnPage { get; set; } public bool HasNextPage { get; set; } public

Random “Missing type map configuration or unsupported mapping.” Error in Automapper

筅森魡賤 提交于 2019-12-02 09:37:35
Please see this post for the solution. Ok, I finally figured it out. The: AppDomain.CurrentDomain.GetAssemblies() piece of my code sometimes does not get my mapping assembly so while it is missing I get an error. Replacing this code by forcing the app to find all assemblies solved my problem. My Entity: /// <summary> /// Get/Set the name of the Country /// </summary> public string CountryName { get; set; } /// <summary> /// Get/Set the international code of the Country /// </summary> public string CountryCode { get; set; } /// <summary> /// Get/Set the coordinate of the Country /// </summary>

AutoMapper — inheritance mapping not working, same source, multiple destinations

和自甴很熟 提交于 2019-11-30 08:43:22
Can I use inheritance mapping in AutoMapper (v2.2) for maps with the same Source type but different Destination types? I have this basic situation (the real classes have many more properties): public abstract class BaseViewModel { public int CommonProperty { get; set;} } public class ViewModelA : BaseViewModel { public int PropertyA { get; set; } } public class ViewModelB : BaseViewModel { public int PropertyB { get; set; } } ViewModelA and ViewModelB are different representations of the same Entity class: public class Entity { public int Property1 { get; set; } public int Property2 { get; set

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

本小妞迷上赌 提交于 2019-11-30 07:54:30
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 the following code: Mapper.CreateMap<Tag, String>() .ForMember(d => d, o => o.ResolveUsing

Automapper: How to leverage a custom INamingConvention?

会有一股神秘感。 提交于 2019-11-29 16:14:15
I am working with a database where the designers really seemed to enjoy capital letters and the underscore key. Since I have a simple ORM, my data models use these names as well. I need to build DTOs and I would prefer to give them standard names since we are exposing them through services. The code below is now corrected! The test passes so use this as a reference if you need to use multiple naming conventions using System; using System.Collections.Generic; using System.Text.RegularExpressions; using AutoMapper; using NUnit.Framework; namespace AutomapperTest { public class DATAMODEL { public

AutoMapper — inheritance mapping not working, same source, multiple destinations

a 夏天 提交于 2019-11-29 12:21:21
问题 Can I use inheritance mapping in AutoMapper (v2.2) for maps with the same Source type but different Destination types? I have this basic situation (the real classes have many more properties): public abstract class BaseViewModel { public int CommonProperty { get; set;} } public class ViewModelA : BaseViewModel { public int PropertyA { get; set; } } public class ViewModelB : BaseViewModel { public int PropertyB { get; set; } } ViewModelA and ViewModelB are different representations of the same

Automapper expression must resolve to top-level member

你。 提交于 2019-11-29 05:31:22
I am using automapper to map source and destination objects. While I map them I get the below error. Expression must resolve to top-level member. Parameter name: lambdaExpression I am not able resolve the issue. My source and destination objects are: public partial class Source { private Car[] cars; public Car[] Cars { get { return this.cars; } set { this.cars = value; } } } public partial class Destination { private OutputData output; public OutputData Output { get { return this.output; } set { this.output= value; } } } public class OutputData { private List<Cars> cars; public Car[] Cars {

How to configure Conditional Mapping in AutoMapper?

假装没事ソ 提交于 2019-11-28 20:07:09
Suppose I have the following entities (classes) public class Target { public string Value; } public class Source { public string Value1; public string Value2; } Now I want to configure Auto Map, to Map Value1 to Value if Value1 starts with "A", but otherwise I want to map Value2 to Value. This is what I have so far: Mapper .CreateMap<Source,Target>() .ForMember(t => t.Value, o => { o.Condition(s => s.Value1.StartsWith("A")); o.MapFrom(s => s.Value1); <<***But then how do I supply the negative clause!?***>> }) However the part the still eludes me is how to tell AutoMapper to go take s.Value2

Automapper: Hydrate int? based on conditions

你说的曾经没有我的故事 提交于 2019-11-28 12:18:04
问题 I have the following code: [Test] public void ConditionalMapping() { var src = new Sample1 {Age = 1, Number = null}; var dest = new Sample2 {Age = null, Number = 1}; Hydrate(src, dest, false); Assert.That(dest.Age, Is.EqualTo(1)); Assert.That(dest.Number, Is.EqualTo(1)); src = new Sample1 {Age = null, Number = 1}; dest = new Sample2 {Age = 1, Number = null}; Hydrate(src, dest, true); Assert.That(dest.Age, Is.Null); Assert.That(dest.Number, Is.EqualTo(1)); } public void Hydrate(Sample1 src,

Automapper: How to leverage a custom INamingConvention?

走远了吗. 提交于 2019-11-28 09:27:38
问题 I am working with a database where the designers really seemed to enjoy capital letters and the underscore key. Since I have a simple ORM, my data models use these names as well. I need to build DTOs and I would prefer to give them standard names since we are exposing them through services. The code below is now corrected! The test passes so use this as a reference if you need to use multiple naming conventions using System; using System.Collections.Generic; using System.Text