简体   繁体   English

AutoMapper 不映射嵌套复杂类型

[英]AutoMapper not mapping nested complex type

I have the following configuration.我有以下配置。

public class AutoMapperProfile: Profile
    {
        public AutoMapperProfile()
        {
           CreateMap<DTO, Model>();
           CreateMap<InnerDTO, NavigationPropertyModel>();
        }
   }

In my code I have在我的代码中,我有

Model.NavigationProperty = mapper.Map(DTO.InnerDTO, Model.NavigationProperty);

seems to work very well but似乎工作得很好但是

Model = mapper.Map(DTO, Model);

doesn't.没有。 ( InnerDTO isn't mapped) (未映射InnerDTO

PS: mapper is an instance of the automapper. PS:mapper 是 automapper 的一个实例。

I want to stick with the second approach since the DTO can have more properties than just the InnerDTO.我想坚持第二种方法,因为 DTO 可以拥有比 InnerDTO 更多的属性。

I tried using Mapper.AssertConfigurationIsValid();我尝试使用Mapper.AssertConfigurationIsValid(); but got an exception但有一个例外

System.InvalidOperationException: 'Mapper not initialized. System.InvalidOperationException: '映射器未初始化。 Call Initialize with appropriate configuration.使用适当的配置调用 Initialize。 If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance.'如果您尝试通过容器或其他方式使用映射器实例,请确保您没有对静态 Mapper.Map 方法的任何调用,并且如果您使用的是 ProjectTo 或 UseAsDataSource 扩展方法,请确保您传入适当的 IConfigurationProvider实例。'

Try to configure sub-property for CreateMap<DTO, Model>();尝试为CreateMap<DTO, Model>();配置子属性CreateMap<DTO, Model>(); . .

        public AutoMapperProfile()
    {
        CreateMap<DTO, Model>()
            .ForMember(dest => dest.NavigationPropertyModel, opt => opt.MapFrom(src => src.InnerDTO));
        CreateMap<InnerDTO, NavigationPropertyModel>();
    }

Possibly what you are missing is to add your AutoMapperProfile class in Startup.cs .您可能缺少的是在Startup.cs添加AutoMapperProfile类。

public void ConfigureServices(IServiceCollection services) {
    // Automapper conf
    var config = new MapperConfiguration(configure => {
        // Add your profile class here
        configure.AddProfile(new AutoMapperProfile());
    });
    // Creating instance of automapper for dependency injection
    var mapper = config.CreateMapper();
    services.AddSingleton(mapper);

    // More complex code here...
}

Subsequently, by dependency injection, you use automapper.随后,通过依赖注入,您可以使用 automapper。 In my particular case, I do it in the following way:在我的特殊情况下,我按以下方式进行:

[Route("api/Permiso")]
[ApiController]
public class PermisoController : ControllerBase {
    private readonly IMapper _mapper;
    private readonly IBusinessLogicHelper _blh;

    public PermisoController(BusinessLogicHelper blh, IMapper mapper) {
        _blh = blh;
        _mapper = mapper;
    }

    [HttpGet]
    public IActionResult Get() {
        try {
            // Get raw data from entities
            var resultsRaw = _blh.GetDataRaw();
            if (resultsRaw == null) {
                return NotFound();
            }
            // Mapping data from entity to DTO
            var resultsDTO = _mapper.Map<ReturnDataDTO>(resultsRaw);
            return Ok(resultsDTO);
        } catch (Exception ex) {
            // Custom ObjectResult for InternalServerError
            return new InternalServerErrorObjectResult(ex.Message);
        }
    }
}    

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

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