Automapper: How to map IList to EntityCollection
<p>I am using Automapper v1.1, since I am using .NET 3.5 as well as using Entity Framework.</p>

<p>I'm trying to map a ViewModel to Model but keep running into errors. I have the below sample classes:</p>

<p><strong>OrderViewModel</strong></p>

<pre><code>public class OrderViewModel{
  public string OrderNo { get; set; }
  public IList&lt;OrderItem&gt; DisplayItems { get; set; }

  public OrderViewModel(){
    DisplayItems = new List&lt;OrderItem&gt;();
  }

}
</code></pre>

<p><strong>Order (note: this class is auto-generated by EF)</strong></p>

<pre><code>public class Order{
  public string OrderNo { get; set; }
  public EntityCollection&lt;OrderItem&gt; OrderItems { get; set; }

  ...

}
</code></pre>

<p><strong>OrderItem (note: this class is auto-generated by EF)</strong></p>

<pre><code>public class OrderItem{
  public string Name { get; set; }
  public int Quantity { get; set; }
  public int Price { get; set; }

  ...

}
</code></pre>

<p><strong>My Function</strong></p>

<pre><code>public void MyFunction(OrderViewModel source){

  //initialize mapping
  Mapper.CreateMap&lt;OrderViewModel, Order&gt;().ForMember(dest =&gt; dest.OrderItems, opt =&gt; opt.MapFrom(src =&gt; src.DisplayItems));

  //map viewmodel to model
  var model = Mapper.Map&lt;OrderViewModel, Order&gt;(source);

  //does not reach this point
}
</code></pre>

<p>When I tried to trace, it is stopping when it tries to map the DisplayItems to the OrderItems property. I am receiving the following error regarding it: </p>

<blockquote>
  <p>System.InvalidOperationException: Requested operation is now allowed
  when the owner of this RelatedEnd is null. RelatedEnd objects that
  were created with the default constructor should only be used as a
  container during serialization.</p>
</blockquote>

<p>What am I doing wrong? What's the correct way to map an IList to an EntityCollection using Automapper? I am doing this because I do not want to write an iterative code just to copy / transfer items from one collection to the other.</p>