title: AutoMapper ProjectTo<>() not finding map
body: <p>I have a ASP.NET 5 (running on 4.6.2, not Core) application. I wanted to use the ProjectTo&lt;>() method of AutoMapper to project the results from the database to my viewmodels.</p>
  
  <p>I've tried alot of tests, but it seems that the map solely cannot be found when using the ProjectTo&lt;>(). Using mapper.Map&lt;>() on different locations with the same model and viewmodel perfectly works.</p>
  
  <p>I guess there is something wrong with how AutoMapper works with my DI (Autofac), but I can't figure out what.</p>
  
  <p>Anyway, the code:</p>
  
  <p>Startup.Cs</p>
  
  <pre><code> public IServiceProvider ConfigureServices(IServiceCollection services)
          {
              (...)
  
              // Autofac DI
              AutofacContainer = AutofacLoader.Configure(services).Build();
  
              return AutofacContainer.Resolve&lt;IServiceProvider&gt;();
          }
  </code></pre>
  
  <p>AutofacLoader.cs</p>
  
  <pre><code>public static ContainerBuilder Configure(IServiceCollection services)
          {
              var builder = new ContainerBuilder();
  
  (...)
  
  
              // AutoMapper
              builder.RegisterModule&lt;AutoMapperModule&gt;();
  
              if (services != null)
              { 
                  builder.Populate(services);
  
              }
              return builder;
          }
  </code></pre>
  
  <p>AutoMapperModule.cs</p>
  
  <pre><code>public class AutoMapperModule : Module
  {
      protected override void Load(ContainerBuilder builder)
      {
          var mapping = new MapperConfiguration(cfg =&gt;
          {
              cfg.AddProfile(new Core.Mappings.AutoMapperProfileConfiguration());
              cfg.AddProfile(new Dieet.Core.Mappings.AutoMapperProfileConfiguration());
          });
          builder.RegisterInstance(mapping.CreateMapper()).As&lt;IMapper&gt;().AutoActivate();
      }
  }
  </code></pre>
  
  <p>The test that fails with 'Missing map from Patient to PatientViewModel. Create using Mapper.CreateMap'.</p>
  
  <pre><code>   [Fact]
      public async void InfohosServiceReturnsPatientViewModels()
      {
          var db = _container.Resolve&lt;IInfohosDb&gt;();
  
          var search = new PaginatedSearchBase();
          search.OrderBy = "Naam";
  
          var mapper = _container.Resolve&lt;IMapper&gt;();
  
          var result = await search.PagedResultAsAsync&lt;Patient,PatientViewModel &gt;(null,db.Patienten,mapper);
      }
  </code></pre>
  
  <p>PaginatedSearchBase</p>
  
  <pre><code>public class PaginatedSearchBase
  {
      public string OrderBy { get; set; }
      public bool OrderDescending { get; set; }
      public int Page { get; set; } = 1;
      public int PageSize { get; set; } = 10;
  }
  </code></pre>
  
  <p>And finally the extension that calls the ProjectTo</p>
  
  <pre><code>public static class PagedResultExtensions
  {
      public static async Task&lt;PagedResult&lt;T&gt;&gt; PagedResultAsync&lt;T&gt;(this PaginatedSearchBase vm, ICollection&lt;Expression&lt;Func&lt;T, bool&gt;&gt;&gt; whereCollection, IEnumerable&lt;T&gt; context) where T : class
      {
          int totalCount;
          var query = PrepareQuery(vm, whereCollection, context, out totalCount);
  
          return new PagedResult&lt;T&gt;
          {
              Results = await query.ToListAsync(),
              Page = vm.Page,
              PageSize = vm.PageSize,
              Total = totalCount
          };
      }
      public static async Task&lt;PagedResult&lt;TAs&gt;&gt; PagedResultAsAsync&lt;T, TAs&gt;(this PaginatedSearchBase vm, ICollection&lt;Expression&lt;Func&lt;T, bool&gt;&gt;&gt; whereCollection, IEnumerable&lt;T&gt; context, IMapper mapper) where T : class
      {
          int totalCount;
          var query = PrepareQuery(vm, whereCollection, context, out totalCount);
  
          return new PagedResult&lt;TAs&gt;
          {
  ----------&gt; Results = await query.ProjectTo&lt;TAs&gt;(mapper).ToListAsync(),
              Page = vm.Page,
              PageSize = vm.PageSize,
              Total = totalCount
          };
      }
  
      private static IQueryable&lt;T&gt; PrepareQuery&lt;T&gt;(PaginatedSearchBase vm, ICollection&lt;Expression&lt;Func&lt;T, bool&gt;&gt;&gt; whereCollection, IEnumerable&lt;T&gt; context,
          out int totalCount) where T : class
      {
          var query = context.AsQueryable();
          if (whereCollection != null)
          {
              foreach (var w in whereCollection)
              {
                  if (w != null)
                  {
                      query = query.Where(w);
                  }
              }
          }
          // Order by
          query = query.OrderBy($"{vm.OrderBy} {(vm.OrderDescending ? "DESC" : "ASC")}");
  
          // Total rows
          totalCount = query.Count();
  
          // Paging
          query = query.Skip((vm.Page - 1)*vm.PageSize).Take(vm.PageSize);
          return query;
      }
  }
  </code></pre>
  
  <p>For information, I'm using versions:</p>
  
  <ul>
  <li>"Autofac": "4.0.0-rc1-177" </li>
  <li>"Autofac.Extensions.DependencyInjection": "4.0.0-rc1-177"</li>
  <li>"AutoMapper": "4.2.1"</li>
  </ul>
  
  <p><strong>Edit:</strong></p>
  
  <p>A new test that I did to check if the mappings really work:</p>
  
  <pre><code>var mapper = _container.Resolve&lt;IMapper&gt;();
          var p = new Patient();
          p.Naam = "Test";
          var vm = mapper.Map&lt;PatientViewModel&gt;(p);
  
          vm.Naam.ShouldBeEquivalentTo("Test");
  </code></pre>
  
  <p>This test passes</p>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>When I use the Map&lt;> in a Select() instead, it works too, so it's really the ProjectTo&lt;>() that fails:</p>
  
  <pre><code>var results = await query.ToListAsync();
          return new PagedResult&lt;TAs&gt;
          {
              Results = results.Select(mapper.Map&lt;TAs&gt;).ToList(),
              Page = vm.Page,
              PageSize = vm.PageSize,
              Total = totalCount
          };
  </code></pre>
  
  <p>This works, but it requires the mapper to be included and not be injected and it doesn't use the ProjectTo that Automapper has for database access...</p>