Skip to content

What is AutoMapper in ASP.NET Core?

AutoMapper is a simple library that helps us transform one object type into another.
In simple words, AutoMapper is an object-to-object mapper. It maps the properties of two different objects by transforming an input object of one type into the other type.

Why do we need AutoMapper in ASP.NET Core?

If you copy data from one object or model to another by hand, the traditional approach is time-consuming and complex. AutoMapper fixes these issues.
To use AutoMapper, we install the AutoMapper library from the NuGet Package Manager and register it as a service in the Program.cs file.
To map one type to another, AutoMapper looks for mapping profiles, so we create a mapping profile class and derive it from the base Profile class, which has the CreateMap method to map the source and destination objects.

To start, create a Web API project using the Web API template, as shown in the image below. Name the project, then select the default options and the target framework, as shown in the images below.

Selecting the ASP.NET Core Web API project template in Visual Studio
Configuring the Web API project name and location
Web API Additional information dialog with .NET 6.0, controllers and OpenAPI support enabled

Install the AutoMapper package from the NuGet Package Manager, as shown in the image below.

Installing the AutoMapper NuGet package

After installing the packages, your Solution Explorer will look like the image below.

Solution Explorer showing the installed AutoMapper package

Now register AutoMapper in the Program.cs file, as shown in the code and image below.

Registering AutoMapper in Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddAutoMapper(typeof(Program).Assembly);
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseAuthorization();
app.MapControllers();
app.Run();

Now add an Employee model class with the properties below.

public class Employee
    {
        public int Id { get; set; }
        public string Name { get; set; } = String.Empty;
        public string Address { get; set; } = String.Empty;
        public int Salary { get; set; }
        public string Email { get; set; }= String.Empty;
        public DateTime Created { get; set; } 
        public DateTime DOB { get; set; }
    }

Now add an EmployeeDTO class with the properties below.

public class EmployeeDTO
    {
          public string Name { get; set; } = String.Empty;
        public string Address { get; set; } = String.Empty;
        public int Salary { get; set; }
        public string Email { get; set; } = String.Empty;
    }

Now add an AutoMapperProfile class for mapping the objects, with the code below, which uses CreateMap between the source and destination classes.

public class AutoMapperProfile :Profile
   {
       public AutoMapperProfile()
       {
           CreateMap Employee, EmployeeDTO ();
           CreateMap EmployeeDTO, Employee ();
       }
   }
AutoMapperProfile class with CreateMap between Employee and EmployeeDTO

Now create a controller, inject IMapper through the constructor into a readonly field, and write two action methods, one for GET and one for POST, that use the mapper.Map() method to map the objects automatically. Then run the application; it works as expected, as shown in the images below.

Controller using mapper.Map() in the Get and Post action methods
Swagger GET /api/Emp response with employees mapped to DTOs
Swagger POST /api/Emp request body for a new employee
Swagger GET response including the newly added employee