使用自动映射器将复杂的DTO映射到实体

我想从

LDTTicketUploadDTO[] to IEnumerable<LDTTicket>

映射是通过这种方法创建的,最后我映射了数据.

public void UploadLDTTickets(LDTTicketUploadDTO[] ticketDTOs)
        {
            Mapper.CreateMap<LDTTicketUploadDTO, LDTTicket>();
            Mapper.CreateMap<LDTTicketDTO, LDTTicket>();
            Mapper.CreateMap<LDTCustomerDTO, LDTCustomer>();
            Mapper.CreateMap<LDTDeviceDTO, LDTDevice>();
            Mapper.CreateMap<LDTUnitDTO, LDTUnit>();
            Mapper.CreateMap<LDTCommandDTO, LDTCommand>();
            Mapper.CreateMap<LDTCommandParameterDTO, LDTCommandParameter>();
            Mapper.CreateMap<LDTObjectDTO, LDTObject>();
            Mapper.CreateMap<LDTControlFileDTO, LDTControlFile>();
            Mapper.CreateMap<LDTDeviceDTO, LDTDevice>();
            Mapper.CreateMap<LDTLanguageDTO, LDTLanguage>();
            Mapper.CreateMap<LDTObjectBitDTO, LDTObjectBit>();

            var tickets = Mapper.Map<IEnumerable<LDTTicketUploadDTO>, IEnumerable<LDTTicket>>(ticketDTOs);

           // do something with tickets
        }

DTO的结构如下:

 public class LDTTicketUploadDTO
     {
            public LDTTicketDTO Ticket { get; set; }
            public LDTDeviceDTO Device { get; set; }
            public LDTCustomerDTO Customer { get; set; }
     }



 public enum TicketStatus
    {
        New,
        InProgress,
        Done
    }    

    public class LDTTicketDTO
    {
        public bool UploadNeeded { get; set; }
        public string TicketNumber { get; set; }
        public TicketStatus Status { get; set; }
        public string CreatedBy { get; set; }
        public DateTime CreatedOn { get; set; }
        public string AssignedTo { get; set; }
        public IEnumerable<LDTUnitDTO> Units { get; set; }
    }

 public class LDTUnitDTO
    {
        public int Id { get; set; }
        public string FunctionUnit { get; set; }
        public int FunctionUnitAddress { get; set; }
        public string Zone { get; set; }
        public int ZoneUnitAddress { get; set; }
        public string Object { get; set; }
        public int ObjectAddress { get; set; }
        public IEnumerable<LDTCommandDTO> Commands { get; set; }
    }

and more...

起作用的是这些属性已正确映射到其对应实体:

public LDTDeviceDTO Device { get; set; }
public LDTCustomerDTO Customer { get; set; }

不起作用的是未映射此属性:

  public LDTTicketDTO Ticket { get; set; }

实体的结构如下:

public class LDTTicket
    {
        [Key, Column(Order = 0)]
        [Required]
        public string SerialNumber { get; set; }

        [Key, Column(Order = 1)]
        [Required]
        public string TicketNumber { get; set; }

        [Required]
        public DateTime CreatedOn { get; set; }

        [Required]
        public string AssignedTo { get; set; }

        public TicketStatus Status { get; set; }
        public string CreatedBy { get; set; }
        public bool UploadNeeded { get; set; }

        public virtual LDTCustomer Customer { get; set; }
        public virtual LDTDevice Device { get; set; }
        public virtual ICollection<LDTUnit> Units { get; set; }
    }

LDTTicket中仅映射了Customer和Device属性

我的配置有什么问题?

解决方法:

期望在票证上填充LDTTicket子属性,而不是票证本身的匹配属性.从源的票证子属性直接将票证创建到目标的匹配属性,将票证直接映射到票证.注意:您只需要定义一次映射,而不是每个方法执行一次.映射应在应用启动时定义,然后使用.

public void UploadLDTTickets(LDTTicketUploadDTO[] ticketDTOs)
{
    Mapper.CreateMap<LDTTicketUploadDTO, LDTTicket>();
          .ForMember(d => d.SerialNumber, m => m.MapFrom(s => s.Ticket.SerialNumber))
              ...
    //Mapper.CreateMap<LDTTicketDTO, LDTTicket>(); You don't need this
    Mapper.CreateMap<LDTCustomerDTO, LDTCustomer>();
    Mapper.CreateMap<LDTDeviceDTO, LDTDevice>();

    ...
}
上一篇:C#-Automapper和NHibernate:延迟加载


下一篇:CodeGo.net>如何注入与Autofac AutoMapper?