【问题标题】:Automapper not working on nested objectsAutomapper 不适用于嵌套对象
【发布时间】:2018-07-23 18:01:06
【问题描述】:

我目前正在使用 .net core 2.1 并尝试使用 automapper 嵌套对象将模型转换为 dto 并将 dto 转换为模型。当每个字段都正确映射时,关系映射就会出现问题。

型号

public class DropdownValue
{
    public int Id { get; set; }
    public string Value { get; set; }
    public int PropertyId { get; set; }
    public Property Property { get; set; }
}

public class Property
{
    public int Id { get; set; }
    public string Title { get; set; }
    public ValueTypes ValueType { get; set; }
    public InputTypes InputType { get; set; }
    public List<DropdownValue> DropdownValues { get; set; }
}

Dtos

public class DropdownValueDto
{
    public int Id { get; set; }
    public string Value { get; set; }
    public PropertyDto Property { get; set; }
}

public class PropertyDto
{
    public int Id { get; set; }
    public string Title { get; set; }
    public InputTypes InputType { get; set; }
    public ValueTypes ValueType { get; set; }
}

映射器

public class MappingProfile : Profile
{
    public MappingProfile() 
    {
        CreateMap<Property, PropertyDto>();
        CreateMap<DropdownValue, DropdownValueDto>();
    }
}

在处理程序中的使用

_mapper.Map<List<Models.DropdownValue>, List<DropdownValueDto>>(dropdownValues)

【问题讨论】:

  • 哪些属性没有被映射?
  • 只有 DropdownValueDto 中的属性字段。返回空值。期望 PropertyDto
  • 可能是因为源中的Property为null。

标签: c# .net-core automapper


【解决方案1】:

我总是在 .net 4x 框架项目中使用 automapper 映射工具,但是当我开发 .net 核心项目时,我总是使用并推荐 ma​​pster 映射工具。它非常快速和简单! Benchmark Results 它也解决了你的问题。您可以查看下面的示例用法。

首先创建一个映射器类。

public static class Mapper
{
    public static void CreateMap()
    {
        TypeAdapterConfig<Property, PropertyDto>
            .NewConfig();

        TypeAdapterConfig<DropdownValue, DropdownValueDto>
            .NewConfig();
    }
}

在启动时初始化

    public Startup(IHostingEnvironment env)
    {
        // other stuffs

        // Mapping
        Mapper.CreateMap();
    }

用法

dropdownValues.Adapt<List<Models.DropdownValue>, List<DropdownValueDto>>()

【讨论】:

    【解决方案2】:
    //Models
    
    public class DropdownValue
    {
        public int Id { get; set; }
        public string Value { get; set; }
        public int PropertyId { get; set; }
        public Property Property { get; set; } = new Property();
    }
    
    public class Property
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public ValueTypes ValueType { get; set; } = new ValueTypes();
        public InputTypes InputType { get; set; } = new InputTypes();
        public List<DropdownValue> DropdownValues { get; set; } = new List<DropdownValue>();
    }
    //Dtos
    
    public class DropdownValueDto
    {
        public int Id { get; set; }
        public string Value { get; set; }
        public PropertyDto Property { get; set; } = new PropertyDto();
    }
    
    public class PropertyDto
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public InputTypes InputType { get; set; } = new InputTypes();
        public ValueTypes ValueType { get; set; } = new ValueTypes();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-11
      • 1970-01-01
      • 2022-12-10
      • 2017-07-20
      • 1970-01-01
      • 2019-04-13
      • 2019-01-05
      相关资源
      最近更新 更多