user7100073
user7100073

Reputation:

AutoMapper in ASP.Net Core 2.0

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?

Upvotes: 7

Views: 9788

Answers (3)

PRS
PRS

Reputation: 761

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.

Upvotes: 4

Hung Quach
Hung Quach

Reputation: 2177

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>());

Upvotes: 5

user7100073
user7100073

Reputation:

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).

Upvotes: 8

Related Questions