title: DotNet Core 3.1 Web API + AutoMapper: Error in mapper.map method
body: <p>I'm using the Nuget package <a href="https://www.nuget.org/packages/AutoMapper.Extensions.Microsoft.DependencyInjection" rel="nofollow noreferrer">AutoMapper.Extensions.Microsoft.DependencyInjection</a> version 7.0.0, in an ASP.Net Core 3 Web API project.</p>
  
  <p>When I use the Map() method...</p>
  
  <pre class="lang-cs prettyprint-override"><code>IEnumerable&lt;SchoolForListDto&gt; schoolsToReturn = _mapper&lt;IEnumerable&lt;School&gt;, IEnumerable&lt;SchoolForListDto&gt;&gt;.Map(schools);
  </code></pre>
  
  <p>... I receive the following error message:</p>
  
  <pre><code>Type arguments in method IMapper.Map&lt;TDestination&gt;(object) cannot be inferred from the usage. Try specifying the type arguments explicitly.
  </code></pre>
  
  <p>The complete code:</p>
  
  <p>Models/Holder.cs</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Collections.Generic;
  
  namespace SchoolApi.Models
  {
      public class Holder
      {
          public string Id { get; set; }
          public string Name { get; set; }
          public DateTime Created { get; set; }
          public DateTime Updated { get; set; }
          public virtual ICollection&lt;School&gt; Schools { get; set; }
      }
  }
  </code></pre>
  
  <p>Models/School.cs</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Collections.Generic;
  
  namespace SchoolApi.Models
  {
      public class School
      {
          public string Id { get; set; }
          public string Name { get; set; }
          public string PhoneNumber { get; set; }
          public string Email { get; set; }
          public string WebSite { get; set; }
          public string Address { get; set; }
          public string HolderId { get; set; }
          public DateTime Created { get; set; }
          public DateTime Updated { get; set; }
          public virtual Holder Holder { get; set; }
      }
  }
  </code></pre>
  
  <p>Data/DataContext.cs</p>
  
  <pre class="lang-cs prettyprint-override"><code>using Microsoft.EntityFrameworkCore;
  using SchoolApi.Models;
  
  namespace SchoolApi.Data
  {
      public class DataContext : DbContext
      {
          public DbSet&lt;Holder&gt; Holders { get; set; }
          public DbSet&lt;School&gt; Schools { get; set; }
  
          public DataContext(DbContextOptions&lt;DataContext&gt; options)
              : base(options) { }
  
          protected override void OnModelCreating(ModelBuilder builder)
          {
              base.OnModelCreating(builder);
          }
      }
  }
  </code></pre>
  
  <p>Data/IGenericRepository</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System.Collections.Generic;
  using System.Threading.Tasks;
  
  namespace SchoolApi.Data
  {
      public interface IGenericRepository&lt;TEntity&gt; where TEntity : class
      {
          void Add(TEntity entity);
          void Delete(TEntity entity);
          Task&lt;TEntity&gt; Get(object id);
          Task&lt;IEnumerable&lt;TEntity&gt;&gt; GetAll();
          Task&lt;bool&gt; SaveAll();
          void Update(TEntity entity);
      }
  }
  </code></pre>
  
  <p>Data/GenericRepository</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System.Collections.Generic;
  using System.Threading.Tasks;
  using Microsoft.EntityFrameworkCore;
  
  namespace SchoolApi.Data
  {
      public class GenericRepository&lt;TEntity&gt; : IGenericRepository&lt;TEntity&gt; where TEntity : class
      {
          private readonly DataContext _dataContext;
          private readonly string _entityName = typeof(TEntity).FullName;
          public GenericRepository(DataContext dataContext)
          {
              _dataContext = dataContext;
          }
  
          public void Add(TEntity entity)
          {
              _dataContext.Set&lt;TEntity&gt;().Add(entity);
          }
  
          public void Delete(TEntity entity)
          {
              _dataContext.Set&lt;TEntity&gt;().Remove(entity);
          }
  
          public async Task&lt;TEntity&gt; Get(object id)
          {
              TEntity entity = await _dataContext.Set&lt;TEntity&gt;().FindAsync(id);
              return entity;
          }
  
          public async Task&lt;IEnumerable&lt;TEntity&gt;&gt; GetAll()
          {
              IEnumerable&lt;TEntity&gt; entities = await _dataContext.Set&lt;TEntity&gt;().ToListAsync();
              return entities;
          }
  
          public async Task&lt;bool&gt; SaveAll()
          {
              int result = await _dataContext.SaveChangesAsync();
              return result &gt; 0;
          }
  
          public void Update(TEntity entity)
          {
              _dataContext.Set&lt;TEntity&gt;().Update(entity);
          }
      }
  }
  </code></pre>
  
  <p>Dtos/SchoolForListDto.cs</p>
  
  <pre class="lang-cs prettyprint-override"><code>namespace SchoolApi.Dtos
  {
      public class SchoolForListDto
      {
          public string Id { get; set; }
          public string Name { get; set; }
          public string PhoneNumber { get; set; }
          public string Email { get; set; }
          public string HolderName { get; set; }
      }
  }
  </code></pre>
  
  <p>Helpers/AutoMapperProfiles</p>
  
  <pre class="lang-cs prettyprint-override"><code>using AutoMapper;
  using SchoolApi.Dtos;
  using SchoolApi.Models;
  
  namespace SchoolApi.Helpers
  {
      public class AutoMapperProfiles : Profile
      {
          public AutoMapperProfiles()
          {
              CreateMap&lt;School, SchoolForListDto&gt;()
                  .ForMember(dest =&gt; dest.HolderName, opt =&gt; opt.MapFrom(src =&gt; src.Holder.Name));
          }
      }
  }
  </code></pre>
  
  <p>Startup.cs</p>
  
  <pre class="lang-cs prettyprint-override"><code>using AutoMapper;
  using Microsoft.AspNetCore.Builder;
  using Microsoft.AspNetCore.Hosting;
  using Microsoft.EntityFrameworkCore;
  using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.DependencyInjection;
  using Microsoft.Extensions.Hosting;
  using SchoolApi.Data;
  using SchoolApi.Models;
  
  namespace SchoolApi
  {
      public class Startup
      {
          public Startup(IConfiguration configuration)
          {
              Configuration = configuration;
          }
  
          public IConfiguration Configuration { get; }
  
          // This method gets called by the runtime. Use this method to add services to the container.
          public void ConfigureServices(IServiceCollection services)
          {
              services.AddControllers()
                  .AddNewtonsoftJson(options =&gt;
                  {
                      options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
                  });
  
              services.AddCors();
  
              services.AddDbContext&lt;DataContext&gt;(options =&gt; options.UseLazyLoadingProxies().UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
  
              services.AddAutoMapper(typeof(Startup));
  
              services.AddScoped&lt;IGenericRepository&lt;School&gt;, GenericRepository&lt;School&gt;&gt;();
          }
  
          // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
          public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
              }
  
              app.UseCors(x =&gt; x.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
  
              app.UseHttpsRedirection();
  
              app.UseRouting();
  
              app.UseAuthorization();
  
              app.UseEndpoints(endpoints =&gt;
              {
                  endpoints.MapControllers();
              });
          }
      }
  }
  
  </code></pre>
  
  <p>Controllers/SchoolController.cs</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System.Collections.Generic;
  using System.Threading.Tasks;
  using AutoMapper;
  using Microsoft.AspNetCore.Authorization;
  using Microsoft.AspNetCore.Http;
  using Microsoft.AspNetCore.Mvc;
  using Microsoft.Extensions.Logging;
  using SchoolApi.Data;
  using SchoolApi.Dtos;
  using SchoolApi.Models;
  
  namespace SchoolApi.Controllers
  {
      [Route("api/[controller]")]
      [ApiController]
      public class SchoolController : ControllerBase
      {
          private readonly IGenericRepository&lt;School&gt; _repository;
          private readonly IMapper _mapper;
          private readonly ILogger&lt;SchoolController&gt; _logger;
  
          public SchoolController(IGenericRepository&lt;School&gt; repository, IMapper mapper, ILogger&lt;SchoolController&gt; logger)
          {
              _repository = repository;
              _mapper = mapper;
              _logger = logger;
          }
  
          [HttpGet]
          public async Task&lt;IActionResult&gt; GetAll()
          {
              IEnumerable&lt;School&gt; schools = await _repository.GetAll();
              // The error occurs on the following line
              IEnumerable&lt;SchoolForListDto&gt; schoolsToReturn = _mapper&lt;IEnumerable&lt;School&gt;, IEnumerable&lt;SchoolForListDto&gt;&gt;.Map(schools);
              return Ok(schoolsToReturn);
          }
  
          [HttpGet("{id}")]
          public async Task&lt;IActionResult&gt; Get([FromRoute] string id)
          {
              School school = await _repository.Get(id);
              SchoolForListDto schoolToReturn = _mapper.Map&lt;SchoolForListDto&gt;(school);
              return Ok(schoolToReturn);
          }
      }
  }
  </code></pre>
  
  <p>These are the packages that I am using:
  - AutoMapper.Extensions.Microsoft.DependencyInjection (version 7.0.0)
  - Microsoft.AspNetCore.Mvc.NewtonsoftJson (version 3.1.4)
  - Microsoft.EntityFrameworkCore (version 3.1.4)
  - Microsoft.EntityFrameworkCore.Design (version 3.1.4)
  - Microsoft.EntityFrameworkCore.Proxies (version 3.1.4)
  - Microsoft.EntityFrameworkCore.Relational (version 3.1.4)
  - Microsoft.EntityFrameworkCore.SqlServer (version 3.1.4)</p>
  
  <p>Target framework: DotNet Core 3.1
  IDE: Visual Studio Code
  SO: Windows 10
  GitHub repository: <a href="https://github.com/jmcandia/SchoolApi" rel="nofollow noreferrer">https://github.com/jmcandia/SchoolApi</a></p>