ASP.NET Core and AutoMapper

AutoMapper1 is a simple little library built to solve a deceptively complex problem - getting rid of code that mapped one object to another. This type of code is rather dreary and boring to write, so why not invent a tool to do it for us?

1) Install the neccessary NuGet packages

  • PM> Install-Package AutoMapper
  • PM> Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection

2) AutoMapper service

As the next step we need to call the AutoMapper from the startup.cs

3) Register mapping profiles

Create a new class for our mapping profile (on your favorite location) and let this class inherit from Profile.

It is important to know, that the mapping is a one way mapping, if you also need the mapping in the other direction you have to define both mappings.

4) Invoke the mapper

The AutoMapper.Extensions.Microsoft.DependencyInjection package provides AutoMapper through Dependency Injection.

5) Finally

AutoMapper give use a really simple way to map between objects, if you need further informations use the official GitHub Project (very well documented).


Sascha Heyer

Read more posts by this author.

Subscribe to Sascha Heyer, Software Engineer

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!