65.9K
CodeProject is changing. Read more.
Home

ASP.NET Core with automapper

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.19/5 (4 votes)

Apr 10, 2018

CPOL
viewsIcon

11880

How to setup net core with automapper

Introduction

This article should help you to wire up automapper into your project.
I am using .NET Core 2.0 and AutoMapper 6.1.1.

Install

First, install SyrianBallaS.AutoMapper.Extensions.Microsoft.DependencyInjection.Signed nuget package. (I am using version 3.2.0.)

Code

In Startup.cs, add this line to ConfigureServices method.

services.AddAutoMapper();

Result:

Startup.cs

using AutoMapper;

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddAutoMapper();
}

 

Now, you just have to create your Mapping profiles.

Here is an example of mine:

using AutoMapper;

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<V1Response, ConfigurationResponse>()
           .ForMember(a => a.ModelId, b=> b.MapFrom(c=> c.ModelName));
     }
}

Mapping

And here is how you can map your objects.

Here is an example of mine:

var result = Mapper.Map<V1Response, ConfigurationResponse>(v1);

Summary

If you want to know how to setup automapper for ASP MVC, have a look at my previous article.