automapper

Issue with ignoring base class property in child classes mappings using Automapper

只谈情不闲聊 提交于 2019-12-21 17:04:02
问题 I have a scenario where I would like to ignore some properties of classes defined in base class. I have an initial mapping like this Mapper.CreateMap<Node, NodeDto>() .Include<Place, PlaceDto>() .Include<Asset, AssetDto>(); Then I customised it more like this to ignore one of the properties defined in base class NodeDto Mapper.CreateMap<Node, NodeDto>() .ForMember(dest => dest.ChildNodes, opt => opt.Ignore()); However when I try to map, Place to PlaceDto or Asset to AssetDto, the ChildNodes

Automapper map from inner property to destination class

自闭症网瘾萝莉.ら 提交于 2019-12-21 12:03:20
问题 Cant' seem to figure this one out. public class DestinationClass { public int InnerPropertyId { get; set; } public string StrignValue { get; set; } } public class SourceClass { public InnerValue Inner { get; set; } } public class InnerValue { public int InnerPropertyId { get; set; } public string StrignValue {get;set;} } I need to map from SourceClass.InnerValue directly to DestinationClass. How do I do that? Thanks in advance. 回答1: As usual, right after I hit post question button: Mapper

Ignore a property in AutoMapper?

不打扰是莪最后的温柔 提交于 2019-12-21 09:36:17
问题 I'm using Automapper to copy one object properties to other and later will update in database using EF. Question is how to tell Automapper copy every property but ignore a particular property (in this case it will be Id). I'm new to AutoMapper and just have done this code. I don't have other configurations or use of AutoMap in project. Mapper.Map(lead, existingLead); I have downloaded AutoMapper form here https://github.com/AutoMapper/AutoMapper 回答1: On your Mapper.CreateMap<Type1, Type2>()

AutoFixture: how to CreateAnonymous from a System.Type

戏子无情 提交于 2019-12-21 06:58:16
问题 I need to create an object from AutoFixture using nothing more than a System.Type. However, there doesn't appear to be an overload of CreateAnonymous() that simply takes a type. They all expect a compile time generic T. Is there a way to convert a System.Type to T? Edit with usage details: I'm using AutoMapper, which has a hook for injecting components to support complex mapping scenarios: void ConstructServicesUsing(System.Func<Type,object> constructor) As you can see from the signature,

AutoMapper one to many relation

北城以北 提交于 2019-12-21 05:05:13
问题 I'm starting to use AutoMapper for my project. For this I want to do the following 'one-to-many' mapping: Source: public class Team { int Id { get; set; } string TeamName { get; set; } List<Person> Member { get; set; } } public class Person { int Id { get; set; } string Name { get; set; } } Destination: public class TeamDetailsViewModel { int Id { get; set; } string TeamName { get; set; } List<int> MemberIds { get; set; } } How to proceed with AutoMapper? Is this possible? Thanks a lot in

How to update an existing Entity from ViewModel using Automapper and EF4 DbContext with Lazy Loading enabled

帅比萌擦擦* 提交于 2019-12-21 04:48:08
问题 I am using Automapper to map between Entity and ViewModel object (in both directions). The model uses EF4 DbContext POCOs and needs LazyLoading (and therefore Proxy Generation) enabled. I have come across a problem attempting to update an existing entity from a viewmodel. When I call Mapper.Map(vm, entity), Automapper throws an exception. My question is: how are you supposed to work with EF Proxy objects using Automapper? The code looks (simplified) like this: public class MyEntity { public

Automapper and immutability

烂漫一生 提交于 2019-12-21 04:08:41
问题 Is it possible to use AutoMapper with Immutable types? For example my Domain type is immutable and I want to map my view type to this. I believe it is not but just want this confirmed. Also as it is best practice to have your domain types immutable, what is the best practice when mapping your view types to domain types? 回答1: I typically do the mapping from view types to domain types by hand, as I'll typically be working through a more complex interface, using methods and so on. If you use

AutoMapper using the wrong constructor

天大地大妈咪最大 提交于 2019-12-21 03:12:40
问题 Today I upgraded a fully functioning application using AutoMapper v1.1 to now use AutoMapper v2.1 and I am coming across some issues that I never encountered using the previous version. Here is an example of my code mapping back from Dto to Domain object public class TypeOne { public TypeOne() { } public TypeOne(TypeTwo two) { //throw ex if two is null } public TypeOne(TypeTwo two, TypeThree three) { //throw ex if two or three are null } public TypeTwo Two {get; private set;} public TypeThree

Automapper map from nested class to single (flatten)

☆樱花仙子☆ 提交于 2019-12-21 01:20:16
问题 This is my source: public class User { public int UserId { get; set; } public Address Address { get; set; } } public class Address { public string Address { get; set; } public string State {get; set; } } This is my destination: public class UserVM { public int UserId { get; set; } public string Address { get; set; } public string State { get; set; } } How do I do the mapping? The normal create map doesn't work when they say flattening is automatic. 回答1: If you change your destination class

Inject AutoMapper

百般思念 提交于 2019-12-21 01:17:47
问题 I have been working on injecting AutoMapper into controllers. I like the implementation of Code Camp Server. It creates a wrapper around AutoMapper's IMappingEngine. The dependency injection is done using StructureMap. But I need to use Castle Windsor for my project. So, how do we implement the following dependency injection and set-up using Windsor? I am not looking for line-by-line equivalent implementation in Castle Windsor. If you want to do that, please feel free. Instead, what is