title: How to use .Include() with AutoMapper?
body: <p>I'm implementing AutoMapper in my app to have different views for different needs. Until now I've more or less just used the DomainModel.</p>
  
  <p>But when I use AutoMapper for my ViewModel, how do I then .Include() related data?</p>
  
  <p>Startup.cs:</p>
  
  <pre><code>services.AddAutoMapper(typeof(AutoMapperProfiles));
  </code></pre>
  
  <p>AutoMapperProfiles.cs:</p>
  
  <pre><code>public class AutoMapperProfiles : Profile
  {
  
      public AutoMapperProfiles()
      {
  
          Mapper.Initialize(cfg =&gt;
          {
              cfg.CreateMap&lt;Product, ProductVM&gt;();
          });
  
          Mapper.Configuration.AssertConfigurationIsValid();
  
      }
  
  }
  </code></pre>
  
  <p>My model:</p>
  
  <pre><code>public class Product
  {
      public Guid id { get; set; }
      public string Name { get; set; }
      public Guid CategoryId { get; set; }
  }
  
  public class ProductVM
  {
      public Guid id { get; set; }
      public string Name { get; set; }
      public Guid CategoryId { get; set; }
      [ForeignKey("CategoryId")]
      public ProductCategory ProductCategory { get; set; }
  }
  
  public class ProductCategory
  {
      public Guid Id { get; set; }
      public string Name ( get; set; }
  }
  </code></pre>
  
  <p>OnGet:</p>
  
  <pre><code>var products = await _dbContext.Products.ProjectTo&lt;ProductVM&gt;(_mapper.ConfigurationProvider).Include(x =&gt; x.ProductCategory).ToListAsync();
  </code></pre>