Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 681
MJ X Avatar asked Sep 06 '25 09:09

MJ X


2 Answers

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>();
    }
}
like image 131
sun sreng Avatar answered Sep 08 '25 13:09

sun sreng


Using this line instead: typeof(Program).Assembly

like image 23
Tiny Wang Avatar answered Sep 08 '25 15:09

Tiny Wang