automapper

Using Profiles in Automapper to map the same types with different logic

て烟熏妆下的殇ゞ 提交于 2019-12-17 06:35:19
问题 I am using AutoMapper in my ASP.NET MVC website to map my database objects to ViewModel objects and I am trying to use several profiles to map the same types, but using another logic. I had the idea of doing so by reading Matt's blog post where he says: The really key part is the AutoMapper configuration profile. You can group configurations with profiles. Maybe in one profile you format dates in one way, in another profile you format dates in another way. I’m just using one profile here. So

Having AutoMapper to inject dependencies using an IoC Container when needed

陌路散爱 提交于 2019-12-14 04:07:17
问题 I have tried almost everything, but I cannot get AutoMapper to map A => B when B doesn't have a parameterless constructor . I'm using Unity and all the dependencies are registered conveniently but, how do I say to AutoMapper "hey, if the target instance needs some dependency in the constructor, ask Unity to build it, and do the mapping later. I've tried with Mapper.Initialize(configuration => { configuration.ConstructServicesUsing(container.Resolve); configuration.CreateMap<Person,

Migrating to Automapper 4.2.1 from Automapper 2.2.1

纵然是瞬间 提交于 2019-12-14 03:44:49
问题 I have a project where I have been using automapper 2.2.1. I upgraded to 4.2.1 and am completely broken. I tried to follow this by changing all references to Mapper.CreateMap<Source, Target>() to Mapper.Initialize(cfg => { cfg.CreateMap<Source, Target>()}) . I'm now broken bad and I cant seem to get it done.I'm getting " System.Reflection.ReflectionTypeLoadException was unhandled by user code exception with Unable to load one or more of the requested types. Retrieve the LoaderExceptions

AutoMapper and “UseDestinationValue”

萝らか妹 提交于 2019-12-14 03:43:35
问题 What does UseDestinationValue do? I am asking because I have a base and inherited class, and for the base class, I would love to have AutoMapper take existing values for me. Will it do that? (I have looked and the only examples I can see for UseDestinationValue involve lists. Is it only for lists? could I do this: PersonContract personContract = new PersonContract {Name = 'Dan'}; Person person = new Person {Name = "Bob"}; Mapper.CreateMap<PersonContract, Person>() .ForMember(x=>x.Name, opt=

Automapper - ignore all items of IEnumerable<SelectListItem>

痴心易碎 提交于 2019-12-14 03:39:59
问题 Is there anyway of Automapper to ignore all properties of a certain type? We are trying to improve the quality of our code by validating the Automapper mappings but having to put an .Ignore() for all IEnumerable<SelectListItem> which are always manually created is creating friction and slowing down development. Any ideas? Possible Idea after creating mappings: var existingMaps = Mapper.GetAllTypeMaps(); foreach (var property in existingMaps) { foreach (var propertyInfo in property

Html Id question

霸气de小男生 提交于 2019-12-13 21:08:14
问题 I have something like this public class Reminders() { public List<SelectListItem> Reminder { get; set; } public string SelectedReminder { get; set; } } Now I want to make a List of Reminder classes that contain what I have above. List<Reminders> reminders = new List<Reminders>(); // add SelectListItems somehow through auto mapper( not sure how to do) // add reminders into my viewModel Then in my view I would need to loop through them to generate the dropdown list. So the idea is that say if I

Automapper is destroying my application: it is throwing AutomapperException everywhere

◇◆丶佛笑我妖孽 提交于 2019-12-13 18:50:41
问题 Using the automapper should be theoretically very simple: you have two objects with same properties, you map the two and you are done. But no, the automapper is eating my application alive and my patience too. I already opend a question one week ago about a problem I had: Automapper fails to map two entities One week ago it was the exclusive problem I had. I solved it by using Mapper.DynamicMap instead of Mapper.Map instead of Map, which is a pain in the ass... but I don't want to lose hours

Using automapper to map nested objects

北慕城南 提交于 2019-12-13 13:25:48
问题 I have a Customers EF POCO class which contains a reference to the Address table. The following code seems to work, but I'm not sure it's the cleanest way to do this. Is there a better way to map this using only a single Map call? [HttpGet] public ActionResult Details(string ID) { BusinessLogic.Customers blCustomers = new BusinessLogic.Customers("CSU"); DataModels.Customer customer = blCustomers.GetCustomer(ID); CustomerDetailsViewModel model = new CustomerDetailsViewModel(); Mapper.CreateMap

AutoMapper Enum to byte with implemention IMapperConfigurator

两盒软妹~` 提交于 2019-12-13 11:36:39
问题 Enum definition is public enum RowStatusEnum { Modified = 1, Removed = 2, Added = 3 } public class RowStatusEnumConvertor : IMapperConfigurator { public void Cofigure() { Mapper.CreateMap<RowStatusEnum, byte>(); Mapper.CreateMap<byte, RowStatusEnum >(); } } I config autoMapper with Implemention IMapperConfigurator in RowStatusEnumConvertor class, but not work this code and not map this type, i think my config not correct or not enough, please help me thanks 回答1: I have reproduced your problem

Automapper 8 mapping not working properly

三世轮回 提交于 2019-12-13 10:32:59
问题 I have two model classes, when I try to map different properties of different name by using Automapper ForMember method. It throws an automapper configuration validation exception on the mapping of different property. I have tried a lot but It does not help.I do not know why It is throwing an exception when I try to map Quantity property with Quntity property. but when I put same name of the property in both the model classes then it works Below is located all the model classes, exception and