Automapper Error Rest API Missing type map configuration or unsupported mapping

-1

I am encoutering this error with my Automapper. I have no idea where should fix this. Here is the error details

AutoMapper.AutoMapperMappingException HResult=0x80131500
Message=Missing type map configuration or unsupported mapping.
Source= StackTrace:

Here is the startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using ipet_core_api.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace ipet_core_api
{
    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.AddDbContext<TaskUatContext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("TasksUatConnection")));

            services.AddControllers();

            services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

            services.AddScoped<ITaskUatRepo, TaskUatRepo>();

        }

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

Here is the my Automapper Profile

using AutoMapper;
using ipet_core_api.Dtos;
using ipet_core_api.Model;

namespace ipet_core_api.Profiles
{
    public class TaskProfile : Profile
    {
        public TaskProfile()
        {
            //Source -> Target
            CreateMap<TaskModel, TaskReadDto>();
        }
    }
}

Here is my Dto taskReadDto

using System;

namespace ipet_core_api.Dtos
{
    public class TaskReadDto
    {
        
        public int Id { get; set; }
        
        public string Title { get; set; }
        public int PrimaryAssignedTo { get; set; }
        public int SecondaryAssignedTo { get; set; }
        
        public int LinkedID { get; set; }
        public DateTime DueDate { get; set; }
        
        public int TaskPriority { get; set; }
        
        public int CreatedBy { get; set; }
        
        public DateTime Created { get; set; }
        
        public int ModifiedBy { get; set; }
        public DateTime Modifed { get; set; }
        
        public Boolean TaskCompleted { get; set; }
        
        public Boolean Deleted { get; set; }
    }
}

On my controller I have this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using ipet_core_api.Data;
using ipet_core_api.Dtos;
using ipet_core_api.Model;
using Microsoft.AspNetCore.Mvc;

namespace ipet_core_api.Controllers
{
    [Route("api/uat/tasks/[action]")]
    [ApiController]
    public class TaskUatController : ControllerBase
    {
        private readonly ITaskUatRepo _repository;
        private readonly IMapper _mapper;

        public TaskUatController(ITaskUatRepo repository, IMapper mapper)
        {
            _repository = repository;
            _mapper = mapper;
        }
 
       
        //api/uat/tasks/GetTasks?LinkedID=1
        [HttpGet]
        public ActionResult<IEnumerable<TaskReadDto>> GetTasks([FromQuery] int LinkedID)
        {
            var tasksItem = _repository.GetTasks(LinkedID);


            return Ok(_mapper.Map<TaskReadDto>(tasksItem));
        }
    }
}
asp.net
asp.net-web-api
asked on Stack Overflow Sep 23, 2020 by Hawk

1 Answer

0

I figure out the problem

I just realize the I need add IEnumerable to the mapper

from:

return Ok(_mapper.Map<TaskReadDto>(tasksItem));

to:

 return Ok(_mapper.Map<IEnumerable<TaskReadDto> > (tasksItem));
answered on Stack Overflow Sep 23, 2020 by Hawk

User contributions licensed under CC BY-SA 3.0