Author : HASSAN MD TAREQ | Updated : 2020/08/01

What is AutoMapper?

  • AutoMapper is an object-to-object mapper. Object-object mapping works by transforming an input object of one type into an output object of a different type
  • See: AutoMapper Usage Guidelines
  • Automapper Github repo:
  • Automapper API Doc:
  • How do I test my mappings?


Install-Package AutoMapper

Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection


  • you only need one MapperConfiguration instance typically per AppDomain and should be instantiated during startup.
  • a good way to organize your mapping configurations is with profiles. Create classes that inherit from Profile and put the configuration in the constructor
  • if you have only one layer where you need to map between your models, I would recommend not to use AutoMapper Profiles and just go with a simple solution
  • profiles allow you to group common configuration and organize mappings by usage. This lets you put mapping configuration closer to where it’s used, instead of a single file of configuration that becomes impossible to edit/maintain

Simple setup

var config = new MapperConfiguration(cfg => {

    cfg.CreateMap<FooEntity, FooDto>();
    // or
   cfg.CreateMap<FooEntity, FooDto>()
       .ForMember(dest => dest.FullName, opts => opts.MapFrom(src => $"{src.FirstName} {src.LastName}"));
    // ... ... ...

var mapper = config.CreateMapper(); // or IMapper mapper = new Mapper(config);

var fooDto = mapper.Map<FooDto>(fooEntity); // type is inferred: var dest = mapper.Map<FooEntity, FooDto>(...)
Simple setup with profile


public class FooProfile : Profile
  public FooProfile()
   CreateMap<FooEntity, FooDto>()
       .ForMember(dest => dest.FullName, opts => opts.MapFrom(src => $"{src.FirstName} {src.LastName}"));
    // ... ... ...

Using FooProfile

var config = new MapperConfiguration(cfg => {

var mapper = config.CreateMapper();

var fooDto = mapper.Map<FooDto>(fooEntity);

AutoMapper in aspnet core with Dependency Injection

  • Dependency: Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection
  • AddAutoMapper() will scan the aspnet core app project (not library project) and add all profiles derived form AutoMapper Profile (you just need to create profiles)
  • for library project see:
  • see:

AddAutoMapper() in Startup.cs


// ... ... ...

If models are in different package



Get IMapper by DI


public class FooService {

  private readonly IMapper _mapper;
  private readonly IFooRepo _repo;
  public UserController(IMapper mapper, IFooRepo repo) {
    _mapper = mapper;
    _repo = repo;

  public FooDto GetFoo(long id){
    FooEntity fooEntity = _repo.GetFoo(id);
    FooDto fooDto = _mapper.Map<FooDto>();
    return fooDto 
  // ... ... ...


Ignore property

// UpdateEmployeeDto => Employee
CreateMap<UpdateEmployeeDto, Employee>().ForMember(dest => dest.Id, opts => opts.Ignore());

Ignore null values

// Just for compying values
CreateMap<Employee, Employee>().ForMember(dest => dest.Id, opts => opts.Ignore())
    .ForAllMembers(opts => opts.Condition((src, dest, srcMember) => srcMember != null));


  • To set default value for null: