什么是等效于使用AutoMapper 7.0.1在CreateMap中使用“静态” Mapper.Map的“实例”?

时间:2018-09-24 20:09:27

标签: c# automapper automapper-7

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

  

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

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

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方法的映射器实例?

1 个答案:

答案 0 :(得分:1)

使用Lucian的评论,我发现https://stackoverflow.com/a/43259537/64279。似乎有些重载将为您传递一个上下文,该上下文具有 if(el.css('white-space') === 'pre-wrap') { el.css('white-space', 'nowrap'); cell.getRow().reformat(); } else { el.css('white-space', 'pre-wrap'); cell.getRow().normalizeHeight(); } 的实例。

例如:

gcloud

其他方法也有重载,例如

IMapper

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

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

相关问题