Trying to add AutoMapper to Asp.net Core 2?

后端 未结 10 1655
我在风中等你
我在风中等你 2020-12-23 19:20

I worked on a asp.net core 1.1 project a while ago and use in projetc AutoMapper.

in asp.net core 1.1, I add services.AddAutoMapper() in startup file :<

相关标签:
10条回答
  • 2020-12-23 19:46

    In new version (6.1) of AutoMapper.Extensions.Microsoft.DependencyInjection nuget package you should use it as follows:

    services.AddAutoMapper(Type assemblyTypeToSearch);
    // OR
    services.AddAutoMapper(params Type[] assemblyTypesToSearch);
    

    e.g:

    services.AddAutoMapper(typeOf(yourClass)); 
    
    0 讨论(0)
  • 2020-12-23 19:50

    I solved this by creating a class that inherits AutoMapper.Profile

        public class model_to_resource_profile : Profile
        {
            public model_to_resource_profile()
            {
                CreateMap<your_model_class, your_model_resource_class>();
            }
        }
    

    And adding this line in the Startup.cs:

    services.AddAutoMapper(typeof(model_to_resource_profile ));
    
    0 讨论(0)
  • 2020-12-23 19:51

    None of these worked for me, I have a .NET Core 2.2 project and the complete code for configuring the mapper looks like this(part of ConfigureService() method):

        // Auto Mapper Configurations
        var mappingConfig = new MapperConfiguration(mc =>
        {
            mc.AddProfile(new SimpleMappings());
        });
    
        IMapper mapper = mappingConfig.CreateMapper();
        services.AddSingleton(mapper);
    

    Then I have my Mappings class which I've placed in the BL project:

    public class SimpleMappings : Profile
        {
            public SimpleMappings()
            {
                CreateMap<DwUser, DwUserDto>();
                CreateMap<DwOrganization, DwOrganizationDto>();
            }
        }
    

    And finally the usage of the mapper looks like this:

    public class DwUserService : IDwUserService
        {
            private readonly IDwUserRepository _dwUserRepository;
            private readonly IMapper _mapper;
    
            public DwUserService(IDwUserRepository dwUserRepository, IMapper mapper)
            {
                _dwUserRepository = dwUserRepository;
                _mapper = mapper;
            }
    
            public async Task<DwUserDto> GetByUsernameAndOrgAsync(string username, string org)
            {
                var dwUser = await _dwUserRepository.GetByUsernameAndOrgAsync(username, org).ConfigureAwait(false);
                var dwUserDto = _mapper.Map<DwUserDto>(dwUser);
    
                return dwUserDto;
            }
    }
    

    Here is a similar link on the same topic: How to setup Automapper in ASP.NET Core

    0 讨论(0)
  • 2020-12-23 19:51

    Install package:

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

    Nuget:
    https://www.nuget.org/packages/AutoMapper.Extensions.Microsoft.DependencyInjection/

    In Startup Class:

    services.AddAutoMapper(typeof(Startup));
    
    0 讨论(0)
  • 2020-12-23 19:52

    If you are using AspNet Core 2.2 and AutoMapper.Extensions.Microsoft.DependencyInjection v6.1 You need to use in Startup file

    services.AddAutoMapper(typeof(Startup));
    
    0 讨论(0)
  • 2020-12-23 19:52

    The official docs: https://automapper.readthedocs.io/en/latest/Dependency-injection.html#asp-net-core

    You define the configuration using profiles. And then you let AutoMapper know in what assemblies are those profiles defined by calling the IServiceCollection extension method AddAutoMapper at startup:

    services.AddAutoMapper(profileAssembly1, profileAssembly2 /*, ...*/);
    

    or marker types:

    services.AddAutoMapper(typeof(ProfileTypeFromAssembly1), typeof(ProfileTypeFromAssembly2) /*, ...*/);
    
    0 讨论(0)
提交回复
热议问题