Before starting this article I would like to give a definition of AutoMapper. AutoMapper is an object-object mapper that allows you to solve the problem of manually mapping each property of a class with the same properties of another class.
Before AutoMapper was introduced if we wanted to assign one object property to another object property then we were following a long procedure. We had to map each property of these two different objects. Suppose in one class we have 30 properties and we want to map this with another class having 30 properties, then we would have to map this property one-by-one 30 times.
Thus for this problem, a new concept, AutoMapper, was introduced. It solves this problem using two steps.
Before AutoMapper was introduced if we wanted to assign one object property to another object property then we were following a long procedure. We had to map each property of these two different objects. Suppose in one class we have 30 properties and we want to map this with another class having 30 properties, then we would have to map this property one-by-one 30 times.
Thus for this problem, a new concept, AutoMapper, was introduced. It solves this problem using two steps.
Here I will explain how we can use AutoMapper to perform a simple insert operation.
Create a new MVC project with the following layers. Here I have used Repository layer where all my interfaces are stored.
Here I am not explaining all the layers as you already know I have used Entity Framework with Repository pattern. In Repository Layer I have the interface for a particular operation.
Here is my UI for Registration.

And here I have declared a model as follows.
Here I am not explaining all the layers as you already know I have used Entity Framework with Repository pattern. In Repository Layer I have the interface for a particular operation.
Here is my UI for Registration.

And here I have declared a model as follows.
- public class RegisterModel
- {
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string Password { get; set; }
- public string Email { get; set; }
- public string ImageUrl { get; set; }
- }
Now here is where the interface for the Registration which I have defined inside the Repository layer is.
- namespace MyProjectRepository
- {
- public interface IRegister
- {
- bool Register(RegisterModel reg);
- }
- }
- public class RegisterManager:IRegister
- {
- CodeXEntities _codex = new CodeXEntities();
- public bool Register(RegisterModel reg)
- {
- bool result = false;
- tbl_Registration tbl = new tbl_Registration();
- tbl.Email = reg.Email;
- tbl.FirstName = reg.FirstName;
- tbl.LastName = reg.LastName;
- tbl.Password = reg.Password;
- _codex.tbl_Registration.Add(tbl);
- if (_codex.SaveChanges()==1)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }

This is the manual way of mapping each class property one by one with another class property so it is very complicated when the property of class increases. So to avoid this we have to use Automapper.
To use AutoMapper first install NuGet. Then, install AutoMapper from the package manager console:
- PM> Install-Package AutoMapper
After installing this we have to use the following Namespace.
- using AutoMapper;
Now for using automapper there are two steps:
- Create Map

We can create a map by Mapper.createMap<sourceClass,DestinationClass>();
- Saving the Map Details

Now I am practically using this in this application. So now I have to modify "MyProjectBLL" class as follows to implement AutoMapper.
- public class RegisterManager:IRegister
- {
- CodeXEntities _codex = new CodeXEntities();
- public bool Register(RegisterModel reg)
- {
- bool result = false;
- Mapper.CreateMap<RegisterModel, tbl_Registration>(); //creating map
- var userDto = Mapper.Map<RegisterModel, tbl_Registration>(reg); //
- _codex.tbl_Registration.Add(userDto);
- if (_codex.SaveChanges()==1)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
Now run the application and try to register -- it will work fine.
Thus it will work fine and register the user. So in this way we can use AutoMapper to map different properties of class.
Read more articles on ASP.NET:

nayana csnsPosted Sep 21, 2022, 7:10 AM
Useful. nice article
sanjay kumarPosted Sep 6, 2021, 11:53 AM
Good one, nice explanation
JosephsPosted May 18, 2021, 4:05 PM
Can you share the code
Rikam PalkarPosted May 14, 2020, 12:27 AM
so baseline is both of object has to be exactly same in order to map?
Bhanu KorremulaPosted Feb 27, 2018, 3:05 PM
CreateMap is deprecated. Any suggestions on how to do this for the new version of automapper?
Manav PandyaPosted Jan 16, 2017, 2:33 AM
Thanks for sharing this article
ThrishPosted Mar 16, 2016, 6:59 PM
Nice explanation.
EdPosted Mar 8, 2016, 2:29 PM
I don't think you should be creating a new map on every call. It should be called once, and only once for the entire life of the application.
Sibeesh VenuPosted Mar 7, 2016, 7:32 AM
Nice Share
sreenivasa kPosted Mar 7, 2016, 3:32 AM
very nice once
Debasis SahaPosted Mar 6, 2016, 11:43 PM
Nice one..
Saillesh PawarPosted Mar 6, 2016, 11:01 PM
Good1 Debendara Sir
Humayun Kabir MamunPosted Mar 6, 2016, 10:46 PM
Nice...
Kumaresh RajalingamPosted Mar 6, 2016, 8:45 PM
Nice share
Vignesh ManiPosted Mar 6, 2016, 12:13 PM
Nice
Tom MohanPosted Mar 6, 2016, 12:07 PM
Any performance issues with AutoMapper?
Mohammed IbrahimPosted Mar 6, 2016, 9:55 AM
nice
Surya Pratap SinghPosted Mar 6, 2016, 2:33 AM
Nice ..
Raja TPosted Mar 6, 2016, 2:16 AM
Nice, Thanks for sharing
Asfend YarPosted Mar 6, 2016, 1:45 AM
nice post