Automapper&Entity Framework映射复杂的关系建议

问题描述 投票:0回答:1

我试图映射一个复杂的实体类型,并在嵌套关系时遇到一些问题。

基本上这是我的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; }

这是LDT001201_FILE_STATUS_DTL:

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

这是与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!
c# list automapper hashset flatten
1个回答
1
投票

解:

  1. 将hashset映射到列表GeneralInformation>()
cfg.CreateMap<LDT001002_FILE_MST, 
    .ForMember(dest => dest.FileStatus, opt => opt.MapFrom(
        src => src.LDT001201_FILE_STATUS_DTL));
  1. 将外部引用映射到列表:
 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));
© www.soinside.com 2019 - 2024. All rights reserved.