[英]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.