简体   繁体   English

使用automapper将复杂的DTO映射到实体

[英]Mapping complex DTOs to entities with automapper

I want to map from 我想从中映射

LDTTicketUploadDTO[] to IEnumerable<LDTTicket>

The mappings are created in this method and at the end I map the data. 映射是在此方法中创建的,最后我映射数据。

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
        }

This is how the DTO´s are structured: 这就是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...

What works is that these properties are correctly mapped to their counterpart entities: 有效的是这些属性正确映射到其对应实体:

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

What works NOT is that this property is not mapped: 什么工作不是这个属性没有映射:

  public LDTTicketDTO Ticket { get; set; }

This is how the Entities are structured: 这就是实体的结构:

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; }
    }

ONLY the Customer and Device property are mapped in the LDTTicket 只有 CustomerDevice属性映射到LDTTicket中

What is wrong with my configuration? 我的配置有什么问题?

It's expecting to populate a LDTTicket sub-property on the ticket, not the matching properties of the ticket itself. 它期望在故障单上填充LDTTicket子属性,而不是故障单本身的匹配属性。 Create direct mappings onto the ticket from the Ticket subproperty of the source directly onto the matching properties of the destination. 从源的Ticket子属性直接在目标的匹配属性上创建到故障单的直接映射。 NOTE: You only need to define your mappings once, not per method execution. 注意:您只需要定义一次映射,而不是每个方法执行。 Mappings should be defined at app start up and thereafter used. 映射应在app启动时定义,然后使用。

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

    ...
}

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

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