7

Does someone know if there's any way to use AutoMapper with ASP.Net Core 2.0? There's no extension for IServiceCollection.

And optional question, does anyone tryed to work with AutoMapper with .Net Framework 4.7 or .Net Standard 2.0?

  • 1
    AutoMapper works with ASP.Net Core 2.0. Where do you get stuck exactly? – Win Sep 19 '17 at 15:53
  • 2
    Ugh! It turns out I did not added AutoMapper.Extensions.Microsoft.DependencyInjection. I've added only AutoMapper :/ @Win Thanks for help anyway :) –  Sep 19 '17 at 16:47
  • Possible duplicate of [How to setup Automapper in ASP.NET Core](https://stackoverflow.com/questions/40275195/how-to-setup-automapper-in-asp-net-core) – Michael Freidgeim Dec 08 '17 at 06:15
  • 1
    A complete answer with an example [click this link](https://stackoverflow.com/a/56299926/7487135) – Iman Bahrampour May 25 '19 at 06:11

3 Answers3

8

It turns out you need to add both:
- AutoMapper
- AutoMapper.Extensions.Microsoft.DependencyInjection
or only the 2nd one (which have dependency to the 1st one).

5

You can create an AutoMapperProfile.cs then add to startup.cs like code below

public class AutoMapperProfile : Profile
{
    public AutoMapperProfile()
    {
        CreateMap<Abc, AbcEntity>();
    }              
}

Add to ConfigureServices method in startup.cs

//Automapper profile
Mapper.Initialize(cfg => cfg.AddProfile<AutoMapperProfile>());
Hung Quach
  • 2,079
  • 2
  • 17
  • 28
4

As mentioned above you need the AutoMapper, and AutoMapper.Extensions.Microsoft.DependencyInjection Nuget packages.

Then in your Startup ConfigureServices method if you just add the service using:

services.AddAutoMapper();

This will scan all assemblies within the execution context looking for classes that inherit the Automapper.Profile class and automatically add these to the AutoMapper configuration.

PRS
  • 741
  • 1
  • 7
  • 27