Questions tagged [automapper]

A convention-based object-to-object mapper and transformer for .NET

AutoMapper uses a fluent configuration API to define an object-object mapping strategy. AutoMapper uses a convention-based matching algorithm to match up source to destination values. Currently, AutoMapper is geared towards model projection scenarios to flatten complex object models to DTOs and other simple objects, whose design is better suited for serialization, communication, messaging, or simply an anti-corruption layer between the domain and application layer.

Version 2 introduced several features and had some breaking changes.

Version 3 was focused on a separation for platform deployments, code comments and LINQ projection

Version 4 was released in August 2015, it introduced support for a wide variety of platforms, and consolidated the assemblies so that each platform only referenced one assembly.

Version 5 was released in mid 2016, speed was improved, new initialization methods were introduced and the former ability of modify configurations at runtime was removed (as this could cause problems)

Version 6 was released in 2017, among other changes reverse-mapping support was increased.

Version 7 was released in 2018 and contained some breaking changes and an explicit netstandard 2.0 target.

Version 8 was also released in 2018 and it too has breaking changes (to simplify configuration options, an upgrade guide is provided). It also introduces Value Converters, which provide the ability to define reusable mappers scoped to individual members.

Version 9 was released in 2019, it removed the static mapper (Mapper.Map) and dynamic mapping. Major breaking changes are covered in the upgrade guide

Version 10 is a 2020 release, the upgrade guide indicates changes largely increase the user friendliness of the library with no major breaking changes listed.


Resources


Tutorials

6547 questions
372
votes
9 answers

Ignore mapping one property with Automapper

I'm using Automapper and I have the following scenario: Class OrderModel has a property called 'ProductName' that isn't in the database. So when I try to do the mapping with: Mapper.CreateMap(); It generates an exception : "The…
Msam85
  • 3,964
  • 2
  • 18
  • 18
363
votes
20 answers

How to set up Automapper in ASP.NET Core

I'm relatively new at .NET, and I decided to tackle .NET Core instead of learning the "old ways". I found a detailed article about setting up AutoMapper for .NET Core here, but is there a more simple walkthrough for a newbie?
theutz
  • 11,872
  • 4
  • 17
  • 22
346
votes
4 answers

Automapper: Update property values without creating a new object

How can I use automapper to update the properties values of another object without creating a new one?
ryudice
  • 36,476
  • 32
  • 115
  • 163
244
votes
18 answers

AutoMapper: "Ignore the rest"?

Is there a way to tell AutoMapper to ignore all of the properties except the ones which are mapped explicitly? I have external DTO classes which are likely to change from the outside and I want to avoid specifying each property to be ignored…
Igor Brejc
  • 18,714
  • 13
  • 76
  • 95
220
votes
10 answers

Where to place AutoMapper.CreateMaps?

I'm using AutoMapper in an ASP.NET MVC application. I was told that I should move the AutoMapper.CreateMap elsewhere as they have a lot of overhead. I'm not too sure how to design my application to put these calls in just 1 place. I have a web…
Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
208
votes
4 answers

AutoMapper vs ValueInjecter

Everytime I'm looking for AutoMapper stuff on StackOverflow, I'm reading something about ValueInjecter. Can somebody tell me the pros and cons between them (performance, features, API usage, extensibility, testing) ?
Rookian
  • 19,841
  • 28
  • 110
  • 180
190
votes
6 answers

How to specify mapping rule when names of properties differ

I am a newbie to the Automapper framework. I have a domain class and a DTO class as follows: public class Employee { public long Id {get;set;} public string Name {get;set;} public string Phone {get;set;} public string Fax {get;set;} …
Thomas.Benz
  • 8,381
  • 9
  • 38
  • 65
131
votes
5 answers

Automapper - how to map to constructor parameters instead of property setters

In cases where my destination setters are private, I might want to map to the object using the destination object's constructor. How would you do this using Automapper?
jlembke
  • 13,217
  • 11
  • 42
  • 56
128
votes
16 answers

Automapper missing type map configuration or unsupported mapping - Error

Entity Model public partial class Categoies { public Categoies() { this.Posts = new HashSet(); } public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public…
AliRıza Adıyahşi
  • 15,658
  • 24
  • 115
  • 197
102
votes
6 answers

How to use AutoMapper .ForMember?

I am trying to set up AutoMapper to convert from Entity to DTO. I know I'm supposed to be using .ForMember() after Mapper.CreateMap() to set up custom mappings, but this doesn't seem to be an available method. Edit for clarification: I…
Nellius
  • 3,024
  • 4
  • 20
  • 21
101
votes
4 answers

AutoMapper.Mapper does not contain definition for CreateMap

This might be a basic question but wondering I am not getting AutoMapper.Mapper.CreateMap method. Am I using wrong AutoMapper reference/package? Thanks
Sami
  • 3,686
  • 4
  • 17
  • 28
96
votes
9 answers

AutoMapper convert from multiple sources

Let's say I have two model classes: public class People { public string FirstName {get;set;} public string LastName {get;set;} } Also have a class Phone: public class Phone { public string Number {get;set;} } And I want to convert to a…
Bart Calixto
  • 19,210
  • 11
  • 78
  • 114
95
votes
5 answers

Copy object to object (with Automapper ?)

I have a class: public class Person { public string FirstName { get; set; } public string LastName { get; set; } } I have two instances of Person (person1 and person2). I'd like copy the contents of person2 to person1. I'd like to make…
TheBoubou
  • 19,487
  • 54
  • 148
  • 236
92
votes
6 answers

Mapping Lists using Automapper

I have the classes: public class Person{ /* Props here */ } public class PersonViewModel { /* Props here */ } Then the list: List people = new List(); List peopleVM = Mapper …
Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
92
votes
7 answers

Automapper - Multi object source and one destination

I am using auto mapper to map multiple objects (db class into ui objects). Map 1: Mapper.CreateMap().ForMember(sss => sss.one, m => m.MapFrom(source => source.abc)); Map 2: Mapper.CreateMap
CoolArchTek
  • 3,729
  • 12
  • 47
  • 76
1
2 3
99 100