Currently, I have this Web Api method which manually map my entity to DTO object and it works without issue. Now, I would like to implement AutoMapper which I have installed in registered in in ConfigureServices method and created the AutoMapper profile class and injected automapper in my Web Api controller constructor.
This is my manual mapping without AutoMapper
[HttpGet]
public async Task<ActionResult<IEnumerable<MovieDto>>> GetMovies()
{
//manual mapping which can be replaced with Automapper
var movies = (from m in _context.Movies
select new MovieDto()
{
Id = m.Id,
MovieTitle = m.MovieTitle,
ReleaseDate = m.ReleaseDate,
MovieStatus = m.MovieStatus,
PhotoFile = m.PhotoFile
}).ToListAsync();
return await movies;
}
and this is how I tried to add automapper to replace manual mapping.
[HttpGet]
public async Task<ActionResult<IEnumerable<MovieDto>>> GetMovies()
{
return _mapper.Map<IEnumerable<MovieDto>>(_context.Movies);
}
But, it hits error Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<CinemaApp.NETCore31.WebAPI.Models.MovieDto>' to 'Microsoft.AspNetCore.Mvc.ActionResult<System.Collections.Generic.IEnumerable<CinemaApp.NETCore31.WebAPI.Models.MovieDto>>'
=> _mapper.Map<IEnumerable<MovieDto>>(await _context.Movies.ToListAsync());- Lucian BargaoanuProjectToworks, that's best. - Lucian Bargaoanu