Note that there are some explanatory texts on larger screens.

plurals
  1. POAutoMapper complex object mapping - mapping a list
    text
    copied!<p>I have the following classes. The domain models are created by entity framework and i am using POCO.</p> <pre class="lang-cs prettyprint-override"><code>public class Customer { public int Id { get; set; } public string Name { get; set; } public DateTime CreatedDate{ get; set; } public DateTime ModifiedDate{ get; set; } public virtual ICollection&lt;Order&gt; Orders{ get; set; } } public class CustomerDTO { public int Id { get; set; } public string Name{ get; set; } public List&lt;OrderDTO&gt; Orders{ get; set; } } public class Order { public int Id { get; set; } public string Name { get; set; } public int ProductId { get; set; } public DateTime CreatedDate{ get; set; } public DateTime ModifiedDate{ get; set; } } public class OrderDTO { public int Id { get; set; } public string Name{ get; set; } } </code></pre> <p>I have tried the following mappings.</p> <pre class="lang-cs prettyprint-override"><code>Mapper.CreateMap&lt;Customer, CustomerDTO&gt;(); Mapper.CreateMap&lt;CustomerDTO, Customer&gt;(); Mapper.CreateMap&lt;Order, OrderDTO&gt;(); Mapper.CreateMap&lt;OrderDTO, Order&gt;(); </code></pre> <p>I have also tried </p> <pre class="lang-cs prettyprint-override"><code>Mapper.CreateMap&lt;CustomerDTO, Customer&gt;().ForMember(c =&gt; c.Orders, m =&gt; m.MapFrom ( q =&gt; Mapper.Map&lt;List&lt;OrderDTO&gt;, ICollection&lt;Order&gt;&gt;(q.Orders) ) ); </code></pre> <p>In order to update a customer i retrieve it from the DB and update it with the customerDTO</p> <pre class="lang-cs prettyprint-override"><code>Customer customer = _customerRepository.GetById(customerDTO.Id); Mapper.Map&lt;CustomerDTO, Customer&gt;(customerDTO, customer); </code></pre> <p>The customer object is updating correctly and the created and modified date aren't changed. But each order in the list of orders isnt updating correctly. Its productId, created and modified dates are being set to the default value and not the values that where retrieved from the database.</p> <p>Do i have to do something else because Orders is a virtual collection?</p> <p>I am new to auto mapper any help appreciated.</p> <p><strong>EDIT</strong></p> <p>I added </p> <pre class="lang-cs prettyprint-override"><code>Mapper.AssertConfigurationIsValid(); </code></pre> <p>Received the following error at app start: Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type</p> <p>I updated my mapping to:</p> <pre class="lang-cs prettyprint-override"><code>Mapper.CreateMap&lt;OrderDTO, Order&gt;() .ForMember(x =&gt; x.CreatedDate, y =&gt; y.Ignore()) .ForMember(x =&gt; x.ModifiedDate, y =&gt; y.Ignore()) .ForMember(x =&gt; x.ProductId, y =&gt; y.Ignore()) </code></pre> <p>But the Order is still having the above properties overwritten by the default vale</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