Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
  • Register
  • Login
  1. Home
  2. Blogpost

Implement AutoMapper in Asp.net MVC

Date- Dec 10,2022

6007

Free Download Pay & Download
AutoMapper C#

Hello guys and welcome to Code2Night! Today, we're diving into the world of ASP.NET MVC and exploring a handy tool called AutoMapper. If you've ever found yourself in a situation where you needed to map database objects to other objects, you're in the right place.

Mapping objects manually can be a time-consuming task, especially when dealing with complex data structures. Luckily, AutoMapper comes to the rescue, offering a way to automate the mapping process between similar kinds of objects. This powerful library can save you valuable development time and effort.

In this blog post, we'll take a closer look at how to implement AutoMapper in your ASP.NET MVC application. We'll guide you through the steps required to set it up, configure the mappings, and unleash its potential to simplify your object mapping tasks.

Whether you're a seasoned ASP.NET MVC developer looking for a more efficient way to handle object mapping or a beginner eager to learn about this exciting tool, this blog post will provide you with the knowledge you need to get started with AutoMapper.

By the end of this blog post, you'll have a comprehensive understanding of how to implement AutoMapper in your ASP.NET MVC application. You'll be equipped with the knowledge and tools to automate your object mapping tasks, allowing you to focus on other aspects of your application's development.

So, let's roll up our sleeves and delve into the world of AutoMapper to streamline your object mapping workflow in ASP.NET MVC. Buckle up and get ready to witness the magic of automation! Let's get started on this exciting journey together!

AutoMapper

AutoMapper is a third-party library that helps us to automate the mapping between two similar kinds of objects. For using that we can follow these steps.

First of all, you have to  install the AutoMapper Nuget package that you can see in the image below

AutoMapper

Now click on install and it will ask for permission to install Automapper.


Once the auto mapper is completely installed. You can add one new class to your project.


public class ProductDTO
    {
        public int ProductID { get; set; }
        public string ProductCategory { get; set; }
        public string SubCategory { get; set; }
        public string ProductName { get; set; }
        public string ProductDescription { get; set; }
        public decimal ProductPrice { get; set; }
        public decimal ProductWeight { get; set; }
        public int Units { get; set; }
        public decimal Total { get; set; }
    }

So this is the class we will use in auto mapper mapping. Now you have to add one new class MappingProfile for creating mappings.

using AutoMapper;
using AutoMapperMVC.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AutoMapperMVC
{
    public static class Mapping
    {
        private static readonly Lazy<IMapper> Lazy = new Lazy<IMapper>(() =>
        {
            var config = new MapperConfiguration(cfg => {
                // This line ensures that internal properties are also mapped over.
                cfg.ShouldMapProperty = p => p.GetMethod.IsPublic || p.GetMethod.IsAssembly;
                cfg.AddProfile<MappingProfile>();
            });
            var mapper = config.CreateMapper();
            return mapper;
        });

        public static IMapper Mapper => Lazy.Value;
    }

    public class MappingProfile : Profile
    {
     
        
        public MappingProfile()
        {
              CreateMap<Product, Products>().ReverseMap();
            //CreateMap<Product, ProductDTO>().ForMember(x => x.ProductName, opt => opt.MapFrom(y => y.ProductName)).ReverseMap(); //For custom mappings
        }
    }
}

So in this class, you can create a map for all kinds of objects using the CreateMap method. You can use RevereseMap() to enable reverse mappings.

Now go to your controller and add the following code

using AutoMapper;
using AutoMapperMVC.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AutoMapperMVC.Controllers
{
    public class HomeController : Controller
    {
       
        public HomeController()
        {
           
        }
       
        public ActionResult Index()
        {
            DbEmployeeEntities db = new DbEmployeeEntities();
            List<Product> prodList = db.Products.ToList();
            List<ProductDTO> products = new List<ProductDTO>();
          
            foreach (var item in prodList)
            {
                products.Add( Mapping.Mapper.Map<ProductDTO>(item));
            }
         
            return View();
        }

       
    }
}

So, here you can see how we are using the Mapper. You don't have to configure a mapper for every place and you can simply use the Mapping.Mapper.Map() method wherever you want to use an auto mapper.

So this is how we can implement AutoMapper in asp.net mvc.

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Introduction to C# Programming: Your First Steps in Software Development
Mar 08, 2026
Understanding Lambda Expressions in C#: A Comprehensive Guide
Mar 06, 2026
Get random number in asp.net C#
Dec 23, 2023
Integrate Stripe Payment Gateway In ASP.NET Core 8.0
Nov 23, 2023

Comments

Tags

Swagger UI
Swashbuckle
SwashbuckleAspNetCore
Rest API
Postman
Api Testing
ITextSharp
Export to Pdf
AspNet Core
AspNet
C#
View to Pdf in Aspnet
Scheduler
Fibonacci series in Java
Display Fibonacci Series
First C# Program
What is C?
C
C Programming
CodeLobster
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, India   info@code2night.com

Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • XML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • PDF Editor
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Built with for developers