"instance" 相当于使用 AutoMapper 7.0.1 在 CreateMap 中使用 "static" Mapper.Map 是什么?
What is the "instance" equivalent to using the "static" Mapper.Map inside CreateMap using AutoMapper 7.0.1?
我正在尝试升级到不再使用静态方法的 AutoMapper 7.0.1。我收到以下错误:
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.
我认为它来自这样的配置文件,我切换到不使用静态方法,除了它仍然在 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
方法?
根据Lucian的评论,我找到了。似乎有重载会传递给您一个具有 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 表达式中提供正确数量的参数。
我正在尝试升级到不再使用静态方法的 AutoMapper 7.0.1。我收到以下错误:
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.
我认为它来自这样的配置文件,我切换到不使用静态方法,除了它仍然在 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
方法?
根据Lucian的评论,我找到了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 表达式中提供正确数量的参数。