Mapper not initialized, When Use ProjectTo()

人盡茶涼 提交于 2019-11-30 17:17:09

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 2.1 (AutoMapper 7.0.1) version you have to pass the ConfigurationProvider.

1) Register AutoMapper like described here

2) Inject to the Controller:

private readonly IMapper  _mapper;
public SomeController(ApplicationDbContext dbContext, IMapper mapper)
{ _mapper = mapper; }

3) Pass the ConfigurationProvider this way:

 ApplicationDbContext.SomeEntities.ProjectTo<SomeModel>(_mapper.ConfigurationProvider)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!