Steps to follow for creating app with automepper.
Step.1
create asp.net mvc core project.
Step.2 
Install  AutoMapper.Extensions.Microsoft.DependencyInjection nuget package. 
Note:- sometime  it not take show error but we have to try to clean build or take a new project for automapper extension
Step.3
Add
AutoMapper Services in the ConfigureServices method of the Startup class,
public class Startup { 
  public void ConfigureServices(IServiceCollection
services) 
  { 
   services.AddAutoMapper(typeof(EmployeeProfile));
  } 
}
Step.3
create 5 model classes (poco class) in the model's folder.
1. Employee.cs 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication1.Models
{
    public class Employee
    {
        public int EmployeeId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public DateTime DateOfBrith { get; set; }
       public Gender Gender { get; set; }
        public int DepartmentId { get; set; }
        public string PhotoPath { get; set; }
       public Department Department { get; set; }
    }
}
2. EditEmployeeModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication1.Models
{
    public class EditEmployeeModel
    {
        public int EmployeeId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string ConfirmEmail { get; set; }
        public DateTime DateOfBrith { get; set; }
       public Gender Gender { get; set; }
        public int? DepartmentId { get; set; }
        public string PhotoPath { get; set; }
       public Department Department { get; set; } = new Department();
    }
}
3. EmployeeProfile
using System;
using AutoMapper;
//using EmployeeManagement.Models;
using WebApplication1.Models;
namespace EmployeeManagement.Web.Models
{
    public class EmployeeProfile : Profile
    {
        public EmployeeProfile()
        {
            CreateMap<Employee, EditEmployeeModel>().ForMember(dest => dest.ConfirmEmail, opt => opt.MapFrom(src => src.Email));
            CreateMap<EditEmployeeModel, Employee>();
        }
    }
}
Gender.cs
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication1.Models
{
    public class Gender
    {
        public int Id { get; set; }
        public string Male { get; set; }
        public string Female { get; set; }
    }
}
Department.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication1.Models
{
    public class Department
    {
        public int Id { get; set; }
        public String Deptname { get; set; }
    }
}
EmployeeController
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
    public class EmployeeController : Controller
    {
        private readonly IMapper _mapper;
        public EmployeeController(IMapper mapper)
        {
            _mapper = mapper;
        }
        public IActionResult Index()
        {
            var emp = new Employee { FirstName = "rikesh", LastName = "kumar", Email = "abc@gmail.com" };
            EditEmployeeModel evm = _mapper.Map<EditEmployeeModel>(emp);
            return View(evm);
        }
        //[HttpPost]
        //public Employee Post([FromBody] Employee employee)
        //{
        //    Employee empmodel = new Employee();
        //    empmodel = _mapper.Map<Employee, Employee>(employee);
        //    return empmodel;
        //}
    } 
}
Index.cs
@model WebApplication1.Models.EditEmployeeModel
@{
    ViewData["Title"] = "Index";
}
<h1>Index</h1>
<div>
    <h4>EditEmployeeModel</h4>
    <hr />
    <dl class="row">
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.EmployeeId)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.EmployeeId)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.FirstName)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.FirstName)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.LastName)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.LastName)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.Email)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.Email)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.ConfirmEmail)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.ConfirmEmail)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.DateOfBrith)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.DateOfBrith)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.DepartmentId)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.DepartmentId)
        </dd>
        <dt class = "col-sm-2">
            @Html.DisplayNameFor(model => model.PhotoPath)
        </dt>
        <dd class = "col-sm-10">
            @Html.DisplayFor(model => model.PhotoPath)
        </dd>
    </dl>
</div>
<div>
    @Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
    <a asp-action="Index">Back to List</a>
</div>

 
No comments: