automapper

C# AutoMapper Conditional Mapping based upon target value

人走茶凉 提交于 2019-12-06 20:33:29
问题 Please can any one advise how to use conditional mapping in AutoMapper to map a value in the TARGET object from a SOURCE object based upon an existing TARGET property value? So my source class is: public class UserDetails { public String Nickname { get; set; } } My target class is: public class ProfileViewModel { public Boolean NicknameIsVisible { get; set; public String Nickname { get; set; } } I want to set the "Nickname" property value in the TARGET to match the "Nickname" property value

Automapper - Ignore mapping with condition

白昼怎懂夜的黑 提交于 2019-12-06 18:36:20
问题 I'm using automapper and I would like to know if it's possible to ignore a mapping of a field when that's null. That's my code: .ForMember(dest => dest.BusinessGroup_Id, opt => opt.MapFrom(src => (int)src.BusinessGroup)) src.BusinessGroup type = "enum" dest.BusinessGroup_Id = int The objective it's to ingore that Mapping if src.BusinessGroup = null. 回答1: I think NullSubstitute option will do the trick .ForMember(d => d.BusinessGroup_Id, o => o.MapFrom(s => (int?)s.BusinessGroup)); .ForMember

AutoMapper flattening of nested mappings asks for a custom resolver

天涯浪子 提交于 2019-12-06 18:21:43
问题 I'm somewhat new to AutoMapper and wanted to map a POCO-ish object to a perhaps more complex DTO, the latter tries to be a representation of a Google Books API's Volume resource: Book.cs public class Book { public string Isbn10 { get; set; } public string Isbn13 { get; set; } public string Title { get; set; } public string Author { get; set; } public string Publisher { get; set; } public DateTime Publication { get; set; } public int Pages { get; set; } public string Description { get; set; }

AutoMapper: Why is UseValue only executed once

China☆狼群 提交于 2019-12-06 17:43:38
问题 Why is UseValue only executed once? I need to call the TeamRepository for each request. How can I achieve this? Mapping from TeamEmployee to TeamEmployeeInput: CreateMap<TeamEmployee, TeamEmployeeInput>() .ForMember(x => x.Teams, x => x.UseValue(GetTeamEmployeeInputs())) .ForMember(d => d.SelectedTeam, s => s.MapFrom(x => x.Team == null ? 0 : x.Team.Id)); private IEnumerable<TeamDropDownInput> GetTeamEmployeeInputs() { Team[] teams = CreateDependency<ITeamRepository>().GetAll(); return Mapper

Using automapper to apply a filter to a collection

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 17:04:19
问题 I have a domain model that contains a collection and I want to use AutoMapper to map the parent and children to the view model but I don't want children that have been "soft" deleted to be taken across. For instance: public class Customer { public EntitySet<Order> {get;set;} } public class Order { public DateTime? DeletedDate {get;set;} } my AutoMapper definition would be Mapper.CreateMap<Customer, CustomerViewModel>(); Mapper.CreateMap<Order, OrderViewModel>(); and I don't want Orders to be

AutoMapper failing to map a simple list

心已入冬 提交于 2019-12-06 14:58:53
I have used automapper for mapping lists in the past, for for some reason it won't work in this case. public class MyType1 { public int Id { get; set; } public string Description { get; set; } } public class MyType2 { public int Id { get; set; } public string Description { get; set; } } public void DoTheMap() { Mapper.CreateMap<MyType2, MyType1>(); Mapper.AssertConfigurationIsValid(); var theDto1 = new MyType2() { Id = 1, Description = "desc" }; var theDto2 = new MyType2() { Id = 2, Description = "desc2" }; List<MyType2> type2List = new List<MyType2> { theDto1, theDto2 }; List<MyType1>

Mapping Child Collections using AutoMapper

自古美人都是妖i 提交于 2019-12-06 13:50:13
问题 I am using Automapper for making a copy of an object My domain can be reduced into this following example Consider I have a Store with a collection of Location public class Store { public string Name { get; set;} public Person Owner {get;set;} public IList<Location> Locations { get; set;} } Below is an example of a store instance var source = new Store { Name = "Worst Buy", Owner = new Person { Name= "someone", OtherDetails= "someone" }, Locations = new List<Location> { new Location { Id = 1,

Automapper: How to not repeat mapping config from complex type to base class

感情迁移 提交于 2019-12-06 12:54:16
I have a bunch of DTO classes that inherit from this CardBase : // base class public class CardBase { public int TransId {get; set; } public string UserId { get; set; } public int Shift { get; set; } } // one of the concrete classes public class SetNewCardSettings : CardBase { // specific properties ... } In my MVC project I have a bunch of view models with a AuditVm complex type that has the same properties of CardBase : public class AuditVm { public int TransId {get; set; } public string UserId { get; set; } public int Shift { get; set; } } public class CreateCardVm : CardVm { // specific

Mapping Expression<Func<Type1,bool>> Expression<Func<Type2, bool>>

旧巷老猫 提交于 2019-12-06 11:52:24
I want to pass my query from Business Layer to Service Layer but when doing this i have to convert my DTO to Entity model. Normally i can convert Type1 to Type2 via Autommaper but now i want to map Expression<Func<Type1,bool>> to Expression<Func<Type2, bool>> I got error from Automapper Missing type map configuration or unsupported mapping. Mapping types: Expression`1 -> Expression`1 How can i achieve this? luksan I just updated my answer to the another question you commented on, which I think addresses this: AutoMapper for Func's between selector types As for the error you posted above, that

How to register a custom ObjectMapper with AutoMapper

亡梦爱人 提交于 2019-12-06 11:26:35
I am planning to create my own custom Object Mapper for a type using AutoMapper's IObjectMapper interface, but I don't see any place where we can register the implemented mapper with AutoMapper. Could anyone tell me how to register it. Edit: For more information on this please follow the discussion at AutoMapper-Users group One way to go here is to replace the static registry function in the MapperRegistry class. Here's the current version: public static Func<IEnumerable<IObjectMapper>> AllMappers = () => new IObjectMapper[] { #if !SILVERLIGHT new DataReaderMapper(), #endif new TypeMapMapper