[英]AutoMapper - mapper not found
我似乎无法使用AutoMapper进行任何操作...
首先,文档有误或过时,或者我很愚蠢:
AutoMapperConfiguration.Configure(); // this doesn't exist
其次,我通过调用以下方法创建地图:
Mapper.CreateMap(myType1, myType2)
其中,类型实际上是彼此的精确属性映射。
但是当我打电话
Mapper.Map(myInstanceOf1, myType2)
我收到找不到映射器的错误。 而且,如果我检查AutoMapper内部的_objectMapperCache字典,则可以看到上面映射的内部值为null(因此是例外)。
我究竟做错了什么?
您需要自己创建AutoMapperConfiguration
类。 添加一个静态的Configure
方法,并在其中放置您的配置代码。 例如:
public class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize( x => x.AddProfile<MyProfile>() );
}
}
public class MyProfile : Profile
{
public override string ProfileName
{
get { return "MyProfile"; }
}
public MyProfile()
{
// Configuration here
CreateMap<Account, AccountViewModel>();
}
}
尝试使用通用语法,它对我有用:
Mapper.CreateMap<A, B>().ForMember(....
b = Mapper.Map<A, B>(a);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.