Mapper not initialized, When Use ProjectTo()
You need to pass the MappingConfiguration provider to the ProjectTo call.
public async Task<FreelancerProfileViewModel> GetFreelancerProfile()
{
var id = Guid.Parse(_identity.GetUserId());
var model = await _freelancerProfiles
.AsNoTracking()
.Where(_ => _.User.Id == id)
.ProjectTo<FreelancerProfileViewModel>(_mapper.Configuration)
.FirstAsync();
// var viewmodel = _mapper.Map<FreelancerProfileViewModel>(model);
return model;
}
In the .NET Core 3.1 (AutoMapper 9.0.0) new registration is:
services.AddAutoMapper(typeof(Startup));
In the .NET Core 2.1 (AutoMapper 7.0.1) version you have to pass the ConfigurationProvider
.
Register AutoMapper like described here
Inject to the Controller:
private readonly IMapper _mapper; public SomeController(ApplicationDbContext dbContext, IMapper mapper) { _mapper = mapper; }
Pass the
ConfigurationProvider
this way:ApplicationDbContext.SomeEntities.ProjectTo(_mapper.ConfigurationProvider)