简体   繁体   English

具有依赖注入的 AutoMapper 不映射配置文件?

[英]AutoMapper with Dependency Injection Not Mapping Profiles?

so this is my first Stack Overflow question and I hope do I good job of giving enough detail for help.所以这是我的第一个 Stack Overflow 问题,我希望我能很好地提供足够的帮助细节。 I'm trying to use AutoMappper with dotnet 3.1 with dependency injection however it doesn't seem to be registering my maps the way it says it does.我正在尝试将 AutoMappper 与 dotnet 3.1 与依赖注入一起使用,但它似乎并没有按照它所说的方式注册我的地图。

The error I am receiving:我收到的错误:

AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types:
User -> UserCustomerDto

In my Startup.cs I have: services.AddAutoMapper(typeof(Startup));在我的 Startup.cs 我有: services.AddAutoMapper(typeof(Startup)); Inside the IServiceCollection areaIServiceCollection区域内

I have my User model and my various DTO models created.我有我的User model 和我创建的各种 DTO 模型。

Here is an example Profile model that I have:这是我拥有的示例配置文件 model:

    public class UserCustomerProfile : Profile
    {
        public UserCustomerProfile()
        {
            CreateMap<User, UserCustomerDto>();
            CreateMap<UserCustomerDto, User>();
        }
    }

My DTO Models have all the fields that my User Model has minus a few, the ones I do not want to surface to the UI that is.我的 DTO 模型具有我的用户 Model 所具有的所有字段,这些字段是我不想在 UI 上显示的。

The documentation has been rather difficult to follow as there is examples to non-dependency injection as well as pre-9.0 where configuration was required during the startup of the project.文档很难理解,因为有非依赖注入的示例以及在项目启动期间需要配置的 9.0 之前的示例。

I also tried using .ReverseMap() on the CreateMap() but that didn't work either.我也尝试在CreateMap() ) 上使用.ReverseMap() ,但这也不起作用。

I am injecting inside the service I am using:我在我正在使用的服务中注入:

        private readonly IMapper _mapper;

        public UserService(IMapper mapper)
        {
            _mapper = mapper;
        }

And where the error is spit out is inside that service with this code: _mapper.Map<UserCustomerDto>(user)使用以下代码在该服务内部吐出错误: _mapper.Map<UserCustomerDto>(user)

I'm fairly new to dotnet core, and C# in general so any help would be greatly appreciated.我对 dotnet 核心和 C# 相当陌生,所以任何帮助都将不胜感激。 Thank you and I hope this was informative enough to point me in the right direction.谢谢你,我希望这足以为我指明正确的方向。

UPDATE: When moving the Profile models to the same project as my Startup everything works fine.更新:将 Profile 模型移动到与我的 Startup 相同的项目时,一切正常。 Now I just need to know how I can add reference to the other project containing my Profile models.现在我只需要知道如何添加对包含我的 Profile 模型的其他项目的引用。 Or maybe I can't?或者我不能? Would be nice to keep it in my Models project where the rest of my models are.最好将它保留在我的模型项目中,我的模型的 rest 所在的位置。

I think you haven't registered your automapper Profiles into the services container.我认为您尚未将自动映射器配置文件注册到服务容器中。

The setup for dotnet core is here dotnet core 的设置在这里

https://docs.automapper.org/en/stable/Dependency-injection.html#asp-net-core https://docs.automapper.org/en/stable/Dependency-injection.html#asp-net-core

Add the extra nuget package for dotnet core called AutoMapper.Extensions.Microsoft.DependencyInjection为名为AutoMapper.Extensions.Microsoft.DependencyInjection的 dotnet 核心添加额外的 nuget package

If your profiles are in the same project you can add this code in startup如果您的配置文件在同一个项目中,您可以在启动时添加此代码

services.AddAutoMapper(Assembly.GetExecutingAssembly());

If your automapper profiles are in another project如果您的自动映射器配置文件在另一个项目中

// where UserCustomerProfile is any class in the other project
services.AddAutoMapper(
    Assembly.GetAssembly(typeof(UserCustomerProfile)));

It will search for all profiles and register them into the services list.它将搜索所有配置文件并将它们注册到服务列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM