11/13/2016 - 11:34 AM

AutoMapper Usage in .net core

AutoMapper Usage in .net core

Suppose having SampleDocument and SampleDto. Each type has some fields that other does not have.

And you want to convert one type to other.

First, add dependency:

"AutoMapper": "5.1.1"

Here is what you should do in Startup:

// Before App.UseMvc();

AutoMapper.Mapper.Initialize(cfg => {
  cfg.CreateMap<SampleDocument, SampleDto>();

Source object: SampleDocument, Target object: SampleDto

Automapper ignores the fields that SampleDocument has SampleDto does not have. Automapper also ignores the NullReferenceExceptions, You can define your mapping strategy if you need.

Use it as:


//other example