简体   繁体   English

Automapper中的嵌套集合映射

[英]Nested Collection Mapping in Automapper

Am trying to map nested collections using automapper and I have done the basic setup and configuration. 我正在尝试使用automapper映射嵌套的集合,我已经完成了基本的设置和配置。 When I try to do the map it the nested values are coming as null . 当我尝试进行映射时, 嵌套值将作为null出现 I have tried to follow few posts and put together something. 我试图按照几篇文章整理一些内容。 I want the list to have a hierarchy instead of flattening. 我希望列表具有层次结构而不是扁平化。 Any help around this would be great. 对此的任何帮助将是巨大的。

Source Entities: 源实体:

public class OuterEntity
{
    public int ID { get; set; }
    public string Name { get; set; }
    public List<InnerEntity> InnerEntityList { get; set; }
}

public class InnerEntity
{
    public int InnerId { get; set; }
    public string InnerName { get; set; }
    public List<InnerMostEntity> InnerMostList { get; set; }
}

public class InnerMostEntity
{
    public int InnerMostId { get; set; }
    public string InnerMostName { get; set; }
    public DateTime ModifiedDate { get; set; }
} 

Destination Entities: 目的实体:

public class OuterEntityDTO
{
    public int ID { get; set; }
    public string Name { get; set; }
    public List<InnerEntity> InnerEntityList { get; set; }
}

public class InnerEntityDTO
{
    public int InnerId { get; set; }
    public string InnerName { get; set; }
    public List<InnerMostEntity> InnerMostList { get; set; }
}

public class InnerMostEntityDTO
{
    public int InnerMostId { get; set; }
    public string InnerMostName { get; set; }
    public DateTime ModifiedDate { get; set; }
}

Controller Class: 控制器类别:

public List<OuterEntityDTO> GetAll()
{

var outerEntityList = myRepo.GetAll(); //Type of List<OuterEntity>  

var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<OuterEntity, OuterEntityDTO>().ReverseMap();
    cfg.CreateMap<InnerEntity, InnerEntityDTO>().ReverseMap();
    cfg.CreateMap<InnerMostEntity, InnerMostEntityDTO>().ReveseMap();
});

config.AssertConfigurationIsValid();

var innerMostDTO = Mapper.Map<List<OuterEntity>,List<OuterEntityDTO>>(outerEntityList);

//The inner list at first level itself is null.
return innerMostDTO;
}

Am trying to achieve this in DOT NET Core. 我正在尝试在DOT NET Core中实现这一目标。 Autommaper version is 6.1.1 Autommaper版本是6.1.1

I think you should have a wrong class hierarchy in DTO classes, as you have 我认为您应该在DTO类中使用错误的类层次结构

public List<InnerMostEntity> InnerMostList { get; set; }

in public class InnerEntityDTO, you should write it as 在公共类InnerEntityDTO中,您应该将其编写为

public List<InnerMostEntityDTO> InnerMostList { get; set; }

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

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