automapper

Updated AutoMapper from 3 to 4 broke inheritance mapping

心已入冬 提交于 2019-12-10 15:23:03
问题 I updated AutoMapper from 3.3.1 to 4.0.4 which broke the following mapping with this message Unable to cast object of type 'Foo' to type 'BarDTO' Classes public class FooDTO { // omitted } // derived DTO public class BarDTO : FooDTO { public string Extra { get; set; } } Mapping config Mapper.CreateMap<Foo, FooDTO>().ReverseMap(); Mapper.CreateMap<Foo, BarDTO>(); Mapping Map<Foo, BarDTO>(foo); // throws cast exception I also tried using the .Include() method, but didn't make a difference.

AutoMapper package 5.1.1 is not found when Nuget restore on hosted build agent in Visual Studio Team Services

只愿长相守 提交于 2019-12-10 15:19:14
问题 Really weird problem with restoring AutoMapper on the hosted build agent in Team Services (previously VSO). It just fails with a Warning 'Unable to find version 5.1.1 of package AutoMapper'. We have a custom and private nuget repo so we've had to add a nuget.config to list all the sources but for some reason AutoMapper cannot be found! It works perfectly fine in Visual Studio IDE even after deleting the packages folder. To test out if it is the custom setting with nuget sources, I've created

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

AutoMapper best practices - Should I be asking the DAO for information to fulfill mapping from DTO to domain object?

蓝咒 提交于 2019-12-10 14:47:26
问题 /// <summary> /// Initialize the AutoMapper mappings for the solution. /// http://automapper.codeplex.com/ /// </summary> public static void CreateAutoMapperMaps() { IDaoFactory daoFactory = DependencyResolver.Current.GetService<IDaoFactory>(); Mapper.CreateMap<Error, ErrorDto>() .ReverseMap(); IPlaylistDao playlistDao = daoFactory.GetPlaylistDao(); IUserDao userDao = daoFactory.GetUserDao(); Mapper.CreateMap<Playlist, PlaylistDto>(); Mapper.CreateMap<PlaylistDto, Playlist>() .ForMember

Automapper's condition gets ignored

天大地大妈咪最大 提交于 2019-12-10 14:46:12
问题 Issue Seems like the condition gets ignored. Here is my scenario: Source class public class Source { public IEnumerable<Enum1> Prop1{ get; set; } public IEnumerable<Enum2> Prop2{ get; set; } public IEnumerable<Enum3> Prop3{ get; set; } } The enums subclass from a byte and are decorated with [Flags]. The destination class simply contains properties like Enum1, Enum2 and Enum3 containging the "total" bitwise value. So in essense if the Enumeration contains Enum1.value!, Enum1.Value2 and Enum1

AutoMapper pass parent reference when mapping child object

谁说胖子不能爱 提交于 2019-12-10 14:32:14
问题 I am trying to use AutoMapper to map some DTO (data contract) objects received from a web service into my business objects. The root DTO object contains a collection of child objects. My business object also has a child collection of child business objects. In order to get AutoMapper to work, I had to include a setter on the collection property in my business object or the collection would always be empty. In addition, I had to add a default constructor to the collection type. So, it appears

Automapper Nuget Package failed

青春壹個敷衍的年華 提交于 2019-12-10 14:21:47
问题 I tried to install http://automapper.org/ but this resulted in an error. Install-Package : 'AutoMapper' already has a dependency defined for 'Microsoft.CSharp'. At line:1 char:1 + Install-Package AutoMapper + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Install-Package], InvalidOperationException + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand Any ideas? 回答1: Check your version of nuget package manager is current and if

AutoMapper, how to keep references between mapped objects?

爱⌒轻易说出口 提交于 2019-12-10 13:40:10
问题 I am using AutoMapper to convert a UI model to POCOs that I later serialize to XML using a DataContractSerializer in order to preserve the references between them. The problem comes that, when mapping, the references between those entities are lost . The UI classes reference each other, but the mapping process makes new instances for every reference, so the original relations are broken :( Let me explain: I have 2 entities of type Person Person { List<House> OwnedHouses } And these 2 objects

Automapper inheritance — reusing maps

一个人想着一个人 提交于 2019-12-10 12:55:11
问题 I am trying to use automapper to create a single map for a parent object and to reuse this amongst its children. For child properties I only want to map the extra fields. Is this possible? The code I have looks like this CreateCalculationMap(message); ---- This does the BASE parent mappping. Mapper.CreateMap<QuoteMessage, CalculationGipMessage>() -- This is the child. .Include<QuoteMessage, CalculationBase>() -- Include the parent? .ForMember(a => a.OngoingCommission, b => b.MapFrom(c => c

AutoMapper: Does Map <A,B> give <B,A>?

为君一笑 提交于 2019-12-10 12:34:48
问题 Using Automapper I create a simple map: Mapper.CreateMap<MyCustomerDTO, YourCustomerDTO>() I often need to go the other way too . Do I need to also create the mapping the other way, or will Automapper infer it based on the above mapping? Mapper.CreateMap<YourCustomerDTO, MyCustomerDTO>() //Needed? 回答1: No. you must create two way mapping. A good helper method for two way mapping could be : protected virtual void ViceVersa<T1, T2>() { Mapper.CreateMap<T1, T2>(); Mapper.CreateMap<T2, T1>(); }