【问题标题】:Automapper does not map List<T> to List<T> while mapping ListAutomapper 在映射 List 时不会将 List<T> 映射到 List<T>
【发布时间】:2019-08-19 22:31:21
【问题描述】:

我正在使用带有 CQRS 模式的自动映射器。下面是我的类,它从 .net 核心 API 获取输入。 API 将集合作为输入,我在我的 Mediatr Command 对象中发送集合。在 Mediatr 命令中,我将源集合映射到目标集合,并且在进行映射时,出现以下异常:

AutoMapper.AutoMapperMappingException
  HResult=0x80131500
  Message=Error mapping types.

Inner Exception 1:
AutoMapperMappingException: Missing type map configuration or unsupported mapping.

我正在使用以下代码进行映射:

var insertData = _mapper.Map<List<Source>, List<Destination>>(request.Data.ToList());

在我的课堂上,我有以下内容:

public class Source: ICustomMapping
    {
        public int? Prop1 { get; set; }
        public string Prop2 { get; set; }
        public void CreateMappings(Profile configuration)
        {
            configuration.CreateMap<Destination, Source>()
                .ForMember(dto => dto.Prop1 , opt => opt.MapFrom(p => p.Prop1 ))
                .ForMember(dto => dto.Prop2, opt => opt.MapFrom(p => p.Prop2))
                ;
        }
    }

当我以两种方式(正向和反向)都有单个对象时,此映射完美无缺。现在我需要传递对象集合进行处理并将目标集合数据保存到数据库中。

【问题讨论】:

    标签: automapper


    【解决方案1】:

    查看文档后,我意识到我没有反向映射。

    public void CreateMappings(Profile configuration)
            {
                configuration.CreateMap<Destination, Source>()
                    .ForMember(dto => dto.Prop1 , opt => opt.MapFrom(p => p.Prop1 ))
                    .ForMember(dto => dto.Prop2, opt => opt.MapFrom(p => p.Prop2))
                    .ReverseMap();
            }
    

    我缺少的 ReverseMap()。

    谢谢

    【讨论】:

      猜你喜欢
      • 2016-07-06
      • 2016-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多