0

I am using Automapper v1.1, since I am using .NET 3.5 as well as using Entity Framework.

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

OrderViewModel

public class OrderViewModel{
  public string OrderNo { get; set; }
  public IList<OrderItem> DisplayItems { get; set; }

  public OrderViewModel(){
    DisplayItems = new List<OrderItem>();
  }

}

Order (note: this class is auto-generated by EF)

public class Order{
  public string OrderNo { get; set; }
  public EntityCollection<OrderItem> OrderItems { get; set; }

  ...

}

OrderItem (note: this class is auto-generated by EF)

public class OrderItem{
  public string Name { get; set; }
  public int Quantity { get; set; }
  public int Price { get; set; }

  ...

}

My Function

public void MyFunction(OrderViewModel source){

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

  //map viewmodel to model
  var model = Mapper.Map<OrderViewModel, Order>(source);

  //does not reach this point
}

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:

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.

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.

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
bloodfire1004
  • 493
  • 2
  • 8
  • 24
  • Just *don't* use `EntityCollection`. You don't need to use EF-specific collections to work with EF – Panagiotis Kanavos Aug 19 '16 at 14:31
  • As you'll see in all tutorials, child relations are represented by `ICollection` properties. `ICollection` is the base of all generic collections. EF may load child items using the `EntityCollection` implementation class or not, but user code should *not* use this EF specific class – Panagiotis Kanavos Aug 19 '16 at 14:34

1 Answers1

1

I think it's basically because you're explicitly creating the map from a List to a EntityCollection type - from what I've seen it seems people end up doing this iteratively (I haven't seen a direct cast/conversion yet). But if you want it to work, this approach seems to get the job done.

Doing a quick search you can find some examples where this is managed by using more or less custom mapper code, with iteration. Here's a post with a few examples.

Here's another one that expresses it more clearly.

Good luck!

Community
  • 1
  • 1
SlimsGhost
  • 2,849
  • 1
  • 10
  • 16
  • Aww I had an inkling this was how it would be done but first really wanted to check if there was an alternative. You just confirmed my worst suspicions though, but at least that clears up my mind about this. Here's your bounty! Thanks! – bloodfire1004 Aug 20 '16 at 17:59