首页 > 解决方案 > 映射多个子属性

问题描述

有一些 Automapper 头痛。任何帮助表示赞赏!

public class ObjectA {
  public int Id { get; set; }
  public string Name { get; set; }
  public List<ObjectB> ObjectBList {get; set;}
}

public class ObjectB {
  public int Id { get; set; }
  public int ObjectAId { get; set; }
  public ObjectA Parent;

  public int ObjectCId { get; set; }
  public ObjectC SecondaryNavProperty {get; set;}
}

public ObjectADto {
  public string Name { get; set; }
  public int[] Children { get; set; }
}

如何将 DTO 转换为有效的 ObjectA,其中 ObjectBList 是 ObjectB 的数组,其中每个条目的 Id 都设置为 ObjectDto.Children 属性之一。

我目前正在这样做:

configuration.CreateMap<ObjectADto , ObjectA >()
                .ForMember(destination => ObjectA.ObjectBList , options => options.MapFrom(source => source.Children.Select(detail => detail)))

但这是不对的,因为我没有在哪里对数组 ObjectB.Id中的项目进行评估。作为 .net 核心多对多表存在。ChildrenObjectB

提前致谢!

标签: entity-framework-coreautomapper

解决方案


推荐阅读