Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I know you already got this working but Im going to throw this out there in case other people land here. </p> <p>In AutoMapper, when you have nested objects that need to be mapped, even if they are exactly the same (ie. a contract class and a model class that match), you have to define maps for the child classes, then when defining the map for the parent, inside the '.ForMember' option you can use those child maps to map the parent. I know this may sound confusing but an example will make it clear. </p> <p>Say you have a the following:</p> <pre><code>namespace Contracts.Entities { public class Person { public string FirstName {get; set;} public string LastName {get; set;} public Address Address {get; set;} } public class Address { public string Street {get; set;} public string City {get; set;} public string State {get; set;} } } namespace Model.Entities { public class Person { public string FirstName {get; set;} public string LastName {get; set;} public Address Address {get; set;} } public class Address { public string Street {get; set;} public string City {get; set;} public string State {get; set;} } } </code></pre> <p>Then you go and define the following maps:</p> <pre><code> Mapper.CreateMap&lt;Contracts.Entities.Person, Model.Entities.Person&gt;(); Mapper.CreateMap&lt;Contracts.Entities.Address, Model.Entities.Address&gt;(); </code></pre> <p>You may think that AutoMapper would know to use the Address map when mapping a Contract person to a model person but it does not. Instead, here is what you have to do:</p> <pre><code> Mapper.CreateMap&lt;Contracts.Entities.Person, Model.Entities.Person&gt;() .ForMember(dest =&gt; dest.Address, opt =&gt; opt.MapFrom(src =&gt; Mapper.Map&lt;Contracts.Entities.Address, Model.Entities.Address&gt;(src.Address))); Mapper.CreateMap&lt;Contracts.Entities.Address, Model.Entities.Address&gt;(); </code></pre> <p>So in your case you could define a <code>Mapper.CreateMap&lt;ContactEntity,TransfereeEntity&gt;()</code> map then call that map in the same way as address above when defining the map for PrimaryOriginTransferee. I.E.</p> <pre><code> Mapper.CreateMap&lt;MoveEntity, MoveEntityDto&gt;() .ForMember(dest =&gt; dest.PrimaryOriginTransferee , opt =&gt; opt.MapFrom(src =&gt; Mapper.Map&lt;ContactEntity,TransfereeEntity&gt;(src.PrimaryOriginTransferee ))); </code></pre> <p>Hope this helps someone!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload