已经配置了map,并已依赖注入
cfg.CreateMap<User, MentionUserDto>();
使用 _mapper.Map<IEnumerable<MentionUserDto>>()
正常,但使用 .ProjectTo<MentionUserDto>(_mapper)
总是提示下面的错误:
Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance.
at AutoMapper.Mapper.get_Configuration()
不用依赖注入,使用静态方法配置,则工作正常
AutoMapper.Mapper.Initialize(config =>
{
config.CreateMap<User, MentionUserDto>();
});
改为 .ProjectTo<MentionUserDto>(_mapper.ConfigurationProvider)
就可以了
写了篇相关随笔 .NET Core 中依赖注入 AutoMapper 小记