简体   繁体   English

自动映射器和实体框架映射复杂的关系建议

[英]Automapper & Entity Framework mapping complex relations suggestions

I am trying to map a complex entity type and am hitting some wall when it comes to nested relations. 我正在尝试映射一个复杂的实体类型,并且在涉及嵌套关系时碰壁。

Basically here's my EF entity: 基本上这是我的EF实体:

 public partial class LDT001002_FILE_MST
    {
        public LDT001002_FILE_MST()
        {
            this.LDT001201_FILE_STATUS_DTL = new HashSet<LDT001201_FILE_STATUS_DTL>();
    ... }
        public virtual ICollection<LDT001201_FILE_STATUS_DTL> LDT001201_FILE_STATUS_DTL { get; set; }

This is LDT001201_FILE_STATUS_DTL: 这是LDT001201_FILE_STATUS_DTL:

    public partial class LDT001201_FILE_STATUS_DTL
    {
        public System.Guid FILE_STATUS_DTL_ID { get; set; }
    ...
    }

And here's the viewmodel used with automapper: 这是与automapper一起使用的viewmodel:

 public class GeneralInformation
    {
    ...
        public List<FileStatuses> FileStatuses { get; set; }
    ...
    }

  public class FileStatuses : GeneralInformation { 

        public Guid FILE_STATUS_DTL_ID { get; set; }
        [DisplayName("Date Initiated")]

}

I tried multiple CreateMap configurations but all throw unmapped errors about the object on Mapper.Map for these 2 models... any suggestions from Automapper veterans are welcome!

Solution: 解:

  1. Mapping the hashset to the list GeneralInformation>() 将哈希集映射到列表GeneralInformation>()
cfg.CreateMap<LDT001002_FILE_MST, 
    .ForMember(dest => dest.FileStatus, opt => opt.MapFrom(
        src => src.LDT001201_FILE_STATUS_DTL));
  1. Mapping the external references to the list: 将外部引用映射到列表:
 cfg.CreateMap<LDT001201_FILE_STATUS_DTL, FileStatuses>()
    .ForMember(dest => dest.LOC_ID, opt => opt.MapFrom(src => src.LDT001013_LOC_MST.LOC_ID))
    .ForMember(dest => dest.LOC_NM, opt => opt.MapFrom(src => src.LDT001013_LOC_MST.LOC_NM))
    .ForMember(dest => dest.STATUS_ID, opt => opt.MapFrom(src => src.LDT001601_STATUS_LKP.STATUS_ID))
    .ForMember(dest => dest.STATUS_NM, opt => opt.MapFrom(src => src.LDT001601_STATUS_LKP.STATUS_NM));

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

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