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, 3:10 AM EDT
Useful. nice article
sanjay kumarPosted Sep 6, 2021, 7:53 AM EDT
Good one, nice explanation
JosephsPosted May 18, 2021, 12:05 PM EDT
Can you share the code
Rikam PalkarTOP 100Posted May 13, 2020, 8:27 PM EDT
so baseline is both of object has to be exactly same in order to map?
Bhanu KorremulaPosted Feb 27, 2018, 10:05 AM EST
CreateMap is deprecated. Any suggestions on how to do this for the new version of automapper?
Manav PandyaPosted Jan 15, 2017, 9:33 PM EST
Thanks for sharing this article
ThrishPosted Mar 16, 2016, 2:59 PM EDT
Nice explanation.
EdPosted Mar 8, 2016, 9:29 AM EST
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 VenuTOP 100Posted Mar 7, 2016, 2:32 AM EST
Nice Share
sreenivasa kPosted Mar 6, 2016, 10:32 PM EST
very nice once
Debasis SahaTOP 100Posted Mar 6, 2016, 6:43 PM EST
Nice one..
Saillesh PawarPosted Mar 6, 2016, 6:01 PM EST
Good1 Debendara Sir
Humayun Kabir MamunPosted Mar 6, 2016, 5:46 PM EST
Nice...
Kumaresh RajalingamPosted Mar 6, 2016, 3:45 PM EST
Nice share
Vignesh ManiTOP 100Posted Mar 6, 2016, 7:13 AM EST
Nice
Tom MohanPosted Mar 6, 2016, 7:07 AM EST
Any performance issues with AutoMapper?
Mohammed IbrahimPosted Mar 6, 2016, 4:55 AM EST
nice
Surya Pratap SinghPosted Mar 5, 2016, 9:33 PM EST
Nice ..
Raja TPosted Mar 5, 2016, 9:16 PM EST
Nice, Thanks for sharing
Asfend YarPosted Mar 5, 2016, 8:45 PM EST
nice post