Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET MVC
  4. Implement AutoMapper in Asp.net MVC

Implement AutoMapper in Asp.net MVC

Date- Dec 10,2022 Updated Feb 2026 6121 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.Implement AutoMapper in Aspnet MVC


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

Integrating Authorize.Net Payment Gateway with ASP.NET Core: A Comprehensive Guide
Apr 17, 2026
Implementing Asynchronous Data Access with Dapper in ASP.NET Core
Apr 12, 2026
Connecting ASP.NET Core to DB2: A Step-by-Step Guide
Apr 07, 2026
Integrating ASP.NET Core Identity with NHibernate for Robust User Management
Apr 06, 2026
Previous in ASP.NET MVC
Web Api in Asp.net MVC
Next in ASP.NET MVC
Implement RazorPay Payment Gateway in Asp.net MVC
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    HTTP Error 500.32 Failed to load ASP NET Core runtime 6,938 views
  • 2
    Error-An error occurred while processing your request in .… 11,273 views
  • 3
    Comprehensive Guide to Error Handling in Express.js 235 views
  • 4
    ConfigurationBuilder does not contain a definition for Set… 19,459 views
  • 5
    Mastering JavaScript Error Handling with Try, Catch, and F… 162 views
  • 6
    Mastering Unconditional Statements in C: A Complete Guide … 21,497 views
  • 7
    Unable to connect to any of the specified MySQL hosts 6,232 views

On this page

🎯

Interview Prep

Ace your ASP.NET MVC interview with curated Q&As for all levels.

View ASP.NET MVC Interview Q&As

More in ASP.NET MVC

  • Implement Stripe Payment Gateway In ASP.NET 58743 views
  • Jquery Full Calender Integrated With ASP.NET 39657 views
  • Microsoft Outlook Add Appointment and Get Appointment using … 27583 views
  • How to implement JWT Token Authentication and Validate JWT T… 25286 views
  • Payumoney Integration With Asp.Net MVC 23231 views
View all ASP.NET MVC posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
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 | 1770
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
Code2Night

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

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor