简体   繁体   中英

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:

 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:

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

And here's the viewmodel used with automapper:

 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>()
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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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