3
votes

In a Net Core 3.1 application i' m trying to use the AutoMapper.Extensions.Microsoft.DependencyInjection 7. In solution i have 3 projects:

  • Content (start project)
  • Core
  • Entity framework

After nuget installation, this is my code:

Startup.cs in Content Project:

using AutoMapper;
using Project.Content.EntityFrameWork;
using Project.Content.Dto;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Project.Content
{
    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();
            // Auto Mapper Configurations
            services.AddAutoMapper(typeof(AutoMapping));

            string connectionString = Configuration["ConnectionString:Default"];
            services.AddDbContext<ProjectContext>(options =>
        options.UseSqlServer(connectionString));
        }

        // 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.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

AutoMapping.cs in Content project:

using AutoMapper;
using Project.Content.Core.Domain.Model;

namespace Project.Content.Dto
{
    class AutoMapping : Profile
    {
        public AutoMapping()
        {
            CreateMap<Exercise, ExerciseDto>();
            CreateMap<ExerciseDto, Exercise>();
        }
    }
}

And this is the Controller where I'm trying to map:

using AutoMapper;
using Project.Content.EntityFrameWork;
using Project.Content.Dto;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Project.Content.Controllers
{
    [ApiController]
    [Route("/exercise")]
    public class ExercisesController : ControllerBase
    {
        private readonly ILogger<ExercisesController> _logger;
        private ProjectContext _dbContext;
        private IMapper _mapper;

        public ExercisesController(ILogger<ExercisesController> logger, ProjectContext dbContext, IMapper mapper)
        {
            _logger = logger;
            _dbContext = dbContext;
            _mapper = mapper;
        }

        [HttpGet("{id}")]
        public ExerciseDto GetById(int id)
        {

            var exercise =  _mapper.Map<ExerciseDto>(_dbContext.Exercises.Where(x => x.Id == id));
            return exercise;
        }
    }
}

In this controller when it tries to map objects it shows the error:

AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types: EntityQueryable1 -> ExerciseDto Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable1[[Project.Content.Core.Domain.Model.Exercise, Project.Content.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> Project.Content.Dto.ExerciseDto at lambda_method(Closure , EntityQueryable`1 , ExerciseDto , ResolutionContext ) at lambda_method(Closure , Object , Object , ResolutionContext ) at Project.Content.Controllers.ExercisesController.GetById(Int32 id) in C:\Projects\Project\Project.Content\Project.Content.Service\Controllers\ExercisesController.cs:line 44 at lambda_method(Closure , Object , Object[] ) at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters) at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync() at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync() --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

2

2 Answers

5
votes

You are trying to map the IQueryable<T>, because it uses deferred execution. You need to execute the query by using something like ToList() or ToListAsync() before you attempt to map it.

You are also trying to map a collection to a single item. You should instead map to a collection. The end result looks something like this

[HttpGet("{id}")]
public ExerciseDto GetById(int id)
{
  var exercises =  _dbContext.Exercises.Where(x => x.Id == id).ToList();
  return _mapper.Map<IEnumerable<ExerciseDto>>(exercises);
}

Alternatively, you could leverage AutoMappers Queryable Extensions to perform a projection, which could result in better SQL performance as it will attempt to only query the necessary data. This may look something like so

[HttpGet("{id}")]
public ExerciseDto GetById(int id)
{
  return _mapper.ProjectTo<ExerciseDto>(_dbContext.Exercises.Where(x => x.Id == id)).ToList();
}

As a side note, if you intend for this query to be a single object, or not found if it doesn't exist, you might use FirstOrDefault instead of Where. Also, you can return an IActionResult to leverage base controller results like NotFound or Ok.

0
votes

If you use Automapper package 9.0.0 or superior, you will need to explicitly configure maps.

I resolve this downgrading the AutoMapper to 8.0.0 and AutoMapper.Extensions.Microsoft.DependencyInjection to version 6.0.0