2011-01-20 159 views
0

我似乎无法得到任何与AutoMapper工作...AutoMapper - 映射器没有发现

首先,该文件是错误或过时的,或者说我傻:

AutoMapperConfiguration.Configure(); // this doesn't exist 

其次,我正在通过以下方式创建我的地图:

Mapper.CreateMap(myType1, myType2) 

其中类型是字面上彼此精确的属性图。

但是,当我打电话

Mapper.Map(myInstanceOf1, myType2) 

我得到错误未找到映射。如果我检查AutoMapper内部_objectMapperCache字典,我可以看到上面的映射的内部值为空(因此是例外)。

我在做什么错?

回答

2

尝试使用通用语法,它为我工作:

Mapper.CreateMap<A, B>().ForMember(.... 

b = Mapper.Map<A, B>(a); 
+0

我需要使用ForMember吗?我只想要映射所有属性。 – Jeff 2011-01-20 11:12:16

4

你需要自己创建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>(); 
    } 
}