0

我正在尝试升级到不再使用静态方法的 AutoMapper 7.0.1。我收到以下错误:

映射器未初始化。使用适当的配置调用初始化。如果您尝试通过容器或其他方式使用映射器实例,请确保您没有对静态 Mapper.Map 方法的任何调用,并且如果您使用 ProjectTo 或 UseAsDataSource 扩展方法,请确保传入适当的 IConfigurationProvider实例。

我认为它来自像这样的配置文件,我切换到不使用静态方法,但它仍然使用Mapper.Map<>()lambda 表达式中的静态方法:

public class MyProfile : Profile
{
    public MyProfile()
    {
        CreateMap<CredentialDetailDto, CredentialDetail>()
            .ForMember(x => x.Owners, opt => opt.ResolveUsing(y => 
                Mapper.Map<IList<OwnerDto>>(y.Owners)))
    }
}

如何获取映射器的实例来代替静态Mapper.Map方法?

4

1 回答 1

1

使用 Lucian 的评论,我找到了https://stackoverflow.com/a/43259537/64279。似乎有些重载会传递给您一个具有IMapper.

例如:

.ForMember(x => x.Owners, opt => opt.ResolveUsing((src, dst, arg3, context) => 
    context.Mapper.Map<IList<OwnerDto>>(src.Owners)))

其他方法也有重载,例如

.AfterMap((s, d, context) =>

.ConvertUsing((source, dst, context) =>

您只需要在 lambda 表达式中提供正确数量的参数。

于 2018-09-24T20:42:04.993 回答