Menu

Setting up AutoMapper in asp .net core app using AutoMapperProfile configuration

If you would like to know more about AutoMapper and its usage you can follow this link to AutoMapper web site.

in your Startup.cs file please add the below code under Configure section.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
  app.UseExceptionHandler("/Home/Error");
  Mapper.Initialize(config =>
  {
    config.AddProfile<QuestionProfile>();
    config.AddProfile<AnotherProfile>();
    .......
    .......
  });
}

this is how a profile looks like

using MyProject.Models.Entity;
using MyProject.Models.ViewModel;
using AutoMapper;
namespace MyProject.Models.AutoMapperProfile
{
    public class QuestionProfile : Profile
    {
        public QuestionProfile()
        {
            CreateMap<QuestionViewModel, Question>().ReverseMap();
            CreateMap<QuestionDetailsViewModel, Question>().ReverseMap();
        }
    }
}

Leave a comment