title: EFCore DotNet 5 & Automapper. Map virtual collections
body: <p>Firstly, I am new to Automapper and finding the official documentation poor to say the least.</p>
  <p>I am trying to map two complex entities with virtual collections which themselves need to be mapped</p>
  <pre class="lang-cs prettyprint-override"><code>Map&lt;IEnumerable&lt;ComplexEntityDb&gt;, IEnumerable&lt;ComplexEntityVM&gt;&gt; 
  </code></pre>
  <p>within each row I need to map</p>
  <pre class="lang-cs prettyprint-override"><code>Map&lt;IEnumerable&lt;EntityCollectionItemDb&gt;, IEnumerable&lt;EntityCollectionItemVM&gt;&gt;
  </code></pre>
  <p>A very simplified version of the classes:</p>
  <pre class="lang-cs prettyprint-override"><code>public class ComplexEntityDb
  {
      public int Id;
  
      // Multiple properties (14) removed for brevity
  
      public virtual ICollection&lt;EntityCollectionItemDb&gt; CollectionDb { get; set; }
  }
  
  public class ComplexEntityVM
  {
      public int Id;
  
      // Multiple properties (7) removed for brevity
  
      public virtual ICollection&lt;EntityCollectionItemVM&gt; CollectionDb { get; set; }
  }
  
  
  public class EntityCollectionItemDb
  {
      public int Id;
  
      // Multiple properties (12) removed for brevity
  }
  
  public class EntityCollectionItemVM
  {
      public int Id;
  
      // Multiple properties (6) removed for brevity
  }
  </code></pre>
  <p>What is the correct way to do this with  EF Core 5.04 on dotnet 5 using AutoMapper 10.1.1 and DI extensions 8.1.1</p>
  <p>I have read dozens of articles on this site and there does not seem to be an easy way to do this.</p>
  <p>Many thanks in advance for any help much wiser souls can give.</p>
  <p><strong>EDIT:</strong> (taken from the comment section of @dglozano's answer) -</p>
  <p>I am using mapping profile -</p>
  <pre class="lang-cs prettyprint-override"><code>public class MappingProfile : Profile
  {
      public MappingProfile() 
      {
          CreateMap&lt;ComplexEntityDb, ComplexEntityVM&gt;().ReverseMap();
          CreateMap&lt;EntityCollectionItemDb, EntityCollectionItemVM&gt;).ReverseMap(); 
      } 
  } 
  </code></pre>
  <p>and IMapper -</p>
  <pre class="lang-cs prettyprint-override"><code>private readonly IMapper _mapper; 
  
  public CustomService(IMapper mapper) 
  {
      _mapper = mapper;
  } 
  </code></pre>
  <p>and trying -</p>
  <pre class="lang-cs prettyprint-override"><code>return _mapper.Map&lt;IEnumerable&lt;ComplexEntityDb&gt;, IEnumerable&lt;ComplexEntityVM&gt;&gt;(source); 
  </code></pre>
  <p>Result: <code>EntityCollectionItemVM</code> collection is empty.</p>