c# - Mapping complex DTOs to entities with automapper -


i want map

ldtticketuploaddto[] ienumerable<ldtticket> 

the mappings created in method , @ end map 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);             // tickets         } 

this how dto´s structured:

 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; }     }  , more... 

what works these properties correctly mapped counterpart entities:

public ldtdevicedto device { get; set; } public ldtcustomerdto customer { get; set; } 

what works not property not mapped:

  public ldtticketdto ticket { get; set; } 

this how entities 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 customer , device property mapped in ldtticket

what wrong configuration?

it's expecting populate ldtticket sub-property on ticket, not matching properties of ticket itself. create direct mappings onto ticket ticket subproperty of source directly onto matching properties of destination. note: need define mappings once, not per method execution. mappings should defined @ app start , thereafter used.

public void uploadldttickets(ldtticketuploaddto[] ticketdtos) {     mapper.createmap<ldtticketuploaddto, ldtticket>();           .formember(d => d.serialnumber, m => m.mapfrom(s => s.ticket.serialnumber))               ...     //mapper.createmap<ldtticketdto, ldtticket>(); don't need     mapper.createmap<ldtcustomerdto, ldtcustomer>();     mapper.createmap<ldtdevicedto, ldtdevice>();      ... } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -