AutoMapper in ASP.NET Core + Reverse Mapping

AutoMapper in ASP.NET Core + Reverse Mapping

In this blog, I demonstrate how to use AutoMapper in an ASP.NET Core MVC application with a clear, complete example. I also show manual mapping to help you understand the differences. Watch the full tutorial on YouTube and download the reference PDF.

📺 Watch the Video Tutorial

📄 Download the PDF Guide

Download: AutoMapper in ASP.NET Core MVC (PDF)

🧾 Full C# Code

using System;
using AutoMapper;

namespace StudentMappingApp
{
    // Domain Model
    public class Student
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime DOB { get; set; }
        public string Class { get; set; }
        public string Address { get; set; }
    }

    // DTO
    public class StudentDTO
    {
        public int Id { get; set; }
        public string FullName { get; set; }
        public int Age { get; set; }
        public string Class { get; set; }
        public string Address { get; set; }
    }

    // Manual Mapping
    public static class ManualMapper
    {
        public static StudentDTO Convert(Student student)
        {
            return new StudentDTO
            {
                Id = student.Id,
                FullName = $"{student.FirstName} {student.LastName}",
                Age = CalculateAge(student.DOB),
                Class = student.Class,
                Address = student.Address
            };
        }

        private static int CalculateAge(DateTime dob)
        {
            var today = DateTime.Today;
            int age = today.Year - dob.Year;
            if (dob > today.AddYears(-age)) age--;
            return age;
        }
    }

    // AutoMapper Profile
    public class MappingProfile : Profile
    {
        public MappingProfile()
        {
            CreateMap()
                .ForMember(dest => dest.FullName,
                    opt => opt.MapFrom(src => $"{src.FirstName} {src.LastName}"))
                .ForMember(dest => dest.Age,
                    opt => opt.MapFrom(src => CalculateAge(src.DOB)))
                .ReverseMap()
                .ForMember(dest => dest.FirstName,
                    opt => opt.MapFrom(src => GetFirstName(src.FullName)))
                .ForMember(dest => dest.LastName,
                    opt => opt.MapFrom(src => GetLastName(src.FullName)))
                .ForMember(dest => dest.DOB,
                    opt => opt.Ignore()); // Cannot derive DOB from Age
        }

        private int CalculateAge(DateTime dob)
        {
            var today = DateTime.Today;
            int age = today.Year - dob.Year;
            if (dob > today.AddYears(-age)) age--;
            return age;
        }

        private static string GetFirstName(string fullName)
        {
            if (string.IsNullOrWhiteSpace(fullName)) return "";
            return fullName.Split(' ')[0];
        }

        private static string GetLastName(string fullName)
        {
            if (string.IsNullOrWhiteSpace(fullName)) return "";
            var parts = fullName.Split(' ');
            return parts.Length > 1 ? parts[^1] : "";
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var student = new Student
            {
                Id = 1,
                FirstName = "John",
                LastName = "Doe",
                DOB = new DateTime(2005, 6, 10),
                Class = "10th Grade",
                Address = "123 Main St"
            };

            Console.WriteLine("\n=== AutoMapper Mapping ===");
            var config = new MapperConfiguration(cfg => cfg.AddProfile());
            var mapper = config.CreateMapper();
            var autoDto = mapper.Map(student);
            PrintDTO(autoDto);

            // Map StudentDTO → Student
            var mappedBackStudent = mapper.Map(autoDto);
            Console.WriteLine("\n=== AutoMapper Reverse Mapping ===");
            Console.WriteLine($"FirstName: {mappedBackStudent.FirstName}");
            Console.WriteLine($"LastName: {mappedBackStudent.LastName}");
            Console.WriteLine($"Class: {mappedBackStudent.Class}");
            Console.WriteLine($"Address: {mappedBackStudent.Address}");
            Console.WriteLine($"DOB: {mappedBackStudent.DOB} (Ignored in reverse map)");

            Console.ReadKey();
        }

        static void PrintDTO(StudentDTO dto)
        {
            Console.WriteLine($"Id: {dto.Id}");
            Console.WriteLine($"FullName: {dto.FullName}");
            Console.WriteLine($"Age: {dto.Age}");
            Console.WriteLine($"Class: {dto.Class}");
            Console.WriteLine($"Address: {dto.Address}");
        }
    }
}

📞 Contact Me for 1-to-1 .NET Training

I offer personalized one-to-one .NET training sessions (C#, ASP.NET Core, Web API, Entity Framework and more).

WhatsApp/Call: +91-9331897923

Website: supernovaservices.com

Any Query / Enrollment Request



Google Review Testimonials

.NET Online Training
Average Rating: 4.9
Votes: 50
Reviews: 50