This article demonstrates what Automapper is and how we can integrate Automapper to our ASP.NET Core application.
Download Source code from the given GitHub URL.
Tools Used for development
- Visual Studio 2019
What is AutoMapper?
AutoMapper in C# is a mapper between two objects. That is, AutoMapper is an object-object mapper. It maps the properties of two different objects by transforming the input object of one type to the output object of another type.
Why do we need AutoMapper in C#?
Let’s understand why we need automapper in C# with an example. Let’s say we have two classes such as Employee and EmployeeDTO as shown in the below image.
Step by step walkthroughs
Will see step by step how we can integrate Automapper to a project.
Step 1
Add the AutoMapper.Extensions.Microsoft.DependencyInjection Package to your solution via NuGet.
Step 2
Create two files as Customer.cs and CustomerModel.cs and add class Customer and CustomerModel.
- public class Customer {
- public int CustomerId { get; set; }
- public string CompanyName { get; set; }
- public string FirstName { get; set; }
- public string MiddleName { get; set; }
- public string LastName { get; set; }
- public string Address { get; set; }
- public string Phone { get; set; }
- public string Country { get; set; }
- public string City { get; set; }
- public string Pincode { get; set; }
- }
- public class CustomerModel {
- public int CustomerId { get; set; }
- public string FullName { get; set; }
- public string Phone { get; set; }
- public string Country { get; set; }
- public string City { get; set; }
- public string Pincode { get; set; }
- }
Step 3
Create mapping profile as CustomerProfile.cs file
- public class CustomerProfile : Profile {
- public CustomerProfile () {
- // Mapping properties from Customer to CustomerModel
- CreateMap<Customer, CustomerModel> ()
- .ForMember (dest =>
- dest.FullName,
- opt => opt.MapFrom (src => src.FirstName + " " + src.MiddleName + " " + src.LastName));
- // ForMember is used incase if any field doesn't match
- }
- }
Step 4
Configure AutoMapperConfiguration in the Startup.cs as shown below
- public void ConfigureServices (IServiceCollection services) {
- // ...
- // Auto Mapper Configurations
- var mappingConfig = new MapperConfiguration (mc => {
- mc.AddProfile (new CustomerProfile ());
- });
- IMapper mapper = mappingConfig.CreateMapper ();
- services.AddSingleton (mapper);
- }
Step 5
To invoke the mapped object in code, do something like the following:
- public class HomeController : Controller {
- private readonly IMapper _mapper;
- public HomeController (IMapper mapper) {
- _mapper = mapper;
- }
- public IActionResult Index () {
- Customer customerdetails = new Customer () {
- CustomerId = 1,
- CompanyName = "ABC",
- Address = "Banaglore",
- Phone = "000",
- FirstName = "Shwetha",
- MiddleName = "Amit",
- LastName = "Naik",
- City = "Bangalore",
- Country = "India",
- Pincode = "560091"
- };
- var customerModel = _mapper.Map<CustomerModel> (customerdetails);
- var fullname = customerModel.FullName;
- var address = customerModel.City;
- return View ();
- }
- }
Conclusion
In this article, we have seen what Automapper is and how we can integrate Automapper to our ASP.NET Core application.

Matias LagatPosted Aug 10, 2021, 3:18 AM
Thanks.. I have a question. Can I create another profile e.g SalesProfile then add this as Configuration in the Startup.cs like the CustomerProfile? Do you have any example on this? Thanks a lot.
Burak DoganPosted Mar 22, 2021, 8:43 AM
Thank you so much.