automapper

Automapper with inherited list

久未见 提交于 2019-12-20 07:28:39
问题 FYI: it's different from this question Automapper and inheritance from Collection or List Here is my inherited list: public class MyPagedList<T> : List<T> { public int CurrentPage { get; set; } public int TotalPages { get; set; } public int PageSize { get; set; } public int TotalCount { get; set; } public MyPagedList() { } public MyPagedList(List<T> items, int count, int pageNumber, int pageSize) { TotalCount = count; PageSize = pageSize; CurrentPage = pageNumber; TotalPages = (int)Math

AutoMapper - Conditional Mapping for Type

ε祈祈猫儿з 提交于 2019-12-20 03:45:07
问题 I'd like to do something like the following and I'm wondering if someone knows how to do it: Mapper.CreateMap<Worksheet, V2WorksheetModel>().If(pWorksheet=> pWorksheet.VisitLevel == 2); Mapper.CreateMap<Worksheet, V3WorksheetModel>().If(pWorksheet=> pWorksheet.VisitLevel == 3); Worksheet entityVisit2 = MyService.GetWorksheetByID(100); //visit level 2 Worksheet entityVisit3 = MyService.GetWorksheetByID(150); //visit level 3 WorksheetModelBase modelBase1 = Mapper.Map(entityVisit2);

How to inject AutoMapper with Autofac?

时光总嘲笑我的痴心妄想 提交于 2019-12-20 02:16:18
问题 What is the proper way to inject AutoMapper to other layers? I read this blog post , but this code cause exception below An exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll but was not handled in user code when try mapping in service layer. List<StudentViewModel> list2 = _mapper.Map<List<StudentViewModel>>(list); My AutoFac configuration like below: public static class DependencyRegistration { public static void Config() { var builder = new ContainerBuilder

AutoMapper flatten nested collections

杀马特。学长 韩版系。学妹 提交于 2019-12-19 19:58:20
问题 I try to figure out how to flatten a collection of Merchants, each containing a collection of Orders to a flat List of OrderViewModels. Here my DTO: public class Merchant { public string MerchantName { get; set; } public List<Order> Orders { get; set; } } public class Order { public string OrderId { get; set; } } And Here's the view model: public class OrderViewModel { public string MerchantName { get; set; } public string OrderId { get; set; } } My Goal is to flatten a List<Merchant> to a

AutoMapper's Ignore() not working when using ForSourceMember?

左心房为你撑大大i 提交于 2019-12-19 18:48:13
问题 I'm trying to ignore a property from source type. I have defined mapping like this: var map = AutoMapper.Mapper.CreateMap<Article, IArticle>(); map.ForSourceMember(s => s.DateCreated, opt => opt.Ignore()); map.ForSourceMember(s => s.DateUpdated, opt => opt.Ignore()); When I call Map function, AutoMapper.Mapper.Map(article, articlePoco); destination's properties gets updated anyway. I'm using the latest stable version downloaded from NuGet. Any ideas why this isn't working ? I have found

Extra iterations in a foreach in an AutoMapper map

放肆的年华 提交于 2019-12-19 16:27:47
问题 For some reason, a loop that I use in an AutoMapper map definition is iterating more than it should. The map definition: Mapper.CreateMap<AdminGameEditModel, Game>() .BeforeMap((s, d) => { foreach (var platId in s.PlatformIDs) { Platform newPlat = _gameRepository.GetPlatform(platId); d.Platforms.Add(newPlat); } }) .ForMember(dest => dest.BoxArtPath, opt => opt.Ignore()) .ForMember(dest => dest.IndexImagePath, opt => opt.Ignore()) .ForMember(dest => dest.Cons, opt => opt.MapFrom(src => String

Extra iterations in a foreach in an AutoMapper map

こ雲淡風輕ζ 提交于 2019-12-19 16:27:12
问题 For some reason, a loop that I use in an AutoMapper map definition is iterating more than it should. The map definition: Mapper.CreateMap<AdminGameEditModel, Game>() .BeforeMap((s, d) => { foreach (var platId in s.PlatformIDs) { Platform newPlat = _gameRepository.GetPlatform(platId); d.Platforms.Add(newPlat); } }) .ForMember(dest => dest.BoxArtPath, opt => opt.Ignore()) .ForMember(dest => dest.IndexImagePath, opt => opt.Ignore()) .ForMember(dest => dest.Cons, opt => opt.MapFrom(src => String

Problems trying to attach a new EF4 entity to ObjectContext while its entity collection entities are already attached

寵の児 提交于 2019-12-19 11:47:14
问题 This is somewhat complicated to explain, so please bear with me. I have an ASP.NET MVC 2 project that is slowly killing me in which I'm trying to take form data and translate it into entities to create or update, depending on the context of the situation. The most relevant parts (pseudo-code): Entity Game Scalar properties EntityCollection<Platform> Platforms And the basic workflow is: Form data -> model bound to a DTO -> mapping the DTO to an EF4 entity with AutoMapper. It all works well

Automapper: Using the structure returned by a custom resolver

ⅰ亾dé卋堺 提交于 2019-12-19 08:53:21
问题 With AutoMapper, I am using a ValueResolver that returns a structure like this struct MyStruct { public int propA; public int propB; public int propC; } class MyResolver : ValueResolver<MyViewModel, MyStruct> { protected override MyStruct ResolveCore(MyViewModel source) { ....return MyStruct data } } I returned a structure because the mapping rules are quite complex and I could not write a custom resolver for each properties as they are related to each other. So my idea was to do this in one

Automapper ProjectTo adds ToList into child properties

心已入冬 提交于 2019-12-19 08:43:23
问题 I use projection to map the Entity classes to DTOs using Entity Framework Core. However, projection adds ToList into child collection properties and this slows down the query a lot. Company Entity: public class Company { public Company() { Employees = new List<CompanyEmployee>(); } public string Address { get; set; } public virtual ICollection<CompanyEmployee> Employees { get; set; } ... } Company DTO: public class CompanyDTO { public CompanyDTO() { CompanyEmployees = new List<EmployeeDTO>();