When can we use Mapper.ProjectTo over Mapper.Map in Asp.Net core application? Use of ProjectTo method in Entity Framework Core, will it reduce the number of fields I query from the database to match the model if we add it in the beginning of the _mapper.ProjectTo statement? How will it reduces the query number of fields?
Instead of using this
_mapper.ProjectTo<CompareVarValdto>(_compareRepo.GetCompareDetailsByid(id))
we can use this right?
_mapper.Map<CompareVarValdto>(result)
ComapreVarValdto.cs
public class CompareVarValdto
{
public int CompareVarValId { get; set; }
public int CURRENT_CATEGORY_ID { get; set; }
public int Current_LaunguageId { get; set; }
public int Current_compareDimId { get; set; }
public string CONTENT { get; set; }
}
Controller get method code snippet
[Route("api/[controller]")]
[ApiController]
public class ComapareController : ControllerBase
{
private readonly ICompare _compareRepo;
private readonly IMapper _mapper;
public ComapareController(ICompare compare, IMapper mapper)
{
_compareRepo = compare;
_mapper = mapper;
}
[HttpGet("{id:int}")]
public IActionResult GetComapare(int id)
{
try
{
return Ok(_mapper.ProjectTo<CompareVarValdto>(_compareRepo.GetCompareDetailsByid(id)));
//VS
//var result = _compareRepo.GetCompareDetailsByid(id);
//return Ok(_mapper.Map<CompareVarValdto>(result));
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError,
"Error retrieving data from the database");
}
}
compareRepo Database method
public IQueryable<CompareDim> GetCompareDetailsByid(int compareId)
{
return _context.CompareDim.Include("Launguage");
}