MJ X
MJ X

Reputation: 9044

How to setup AutoMapper in ASP.Net Core 6

How to configure AutoMapper in ASP.Net Core 6

I have a project which is written in .Net 3.1 so we had Startup.cs class.

I am migrating it to .net core 6

now when I put the following configuration in my .Net 6 Program.cs

             builder.Services.AddAutoMapper(typeof(Startup));

I get error the type or namespace Startup could not be found

Any suggestions ?, how can I fix it or configure it in .net 6

Upvotes: 27

Views: 57915

Answers (7)

user25177563
user25177563

Reputation: 21

AutoMapper.Extensions.Microsoft.DependencyInje The display is deprecated

Upvotes: 2

zied ben hadj amor
zied ben hadj amor

Reputation: 1

Try to add this line of code in your Program.cs.

services.AddAutoMapper(Assembly.GetExecutingAssembly());

Upvotes: 0

Islam Helmy
Islam Helmy

Reputation: 21

Use this line to fix:

builder.Services.AddAutoMapper(typeof(Program));

Upvotes: 2

sun sreng
sun sreng

Reputation: 695

install package

  • dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjection

in Program.cs

builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

create MappingProfiles.cs

namespace XXX.XXX.Helpers;

public class MappingProfiles: Profile {
    public MappingProfiles() {
        CreateMap<Address, AddressDto>();
    }
}

Upvotes: 44

Khutso
Khutso

Reputation: 201

For those who didn't know like me.

  1. Install AutoMapper extension for DI via NuGet or by dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjection
  2. Then in the Program.cs file register the service with: builder.Services.AddAutoMapper(typeof(<name-of-profile>)); (In .NET 6 we no longer have the StartUp.cs)

I used Profiles to do my mapping configuration. Here's a link from Automapper about profiles.

Upvotes: 18

Tiny Wang
Tiny Wang

Reputation: 15906

Using this line instead: typeof(Program).Assembly

Upvotes: 29

Vikas
Vikas

Reputation: 67

Install AutoMapper Package(version as per your proj requirement)

Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection -Version 9.0.0

after that register a service in CinfigureServices on Startup.cs

using AutoMapper;
public void ConfigureServices(IServiceCollection services){
    services.AddAutoMapper(typeof(Startup));
}

Upvotes: -2

Related Questions