2010-07-09 32 views
0

我嘗試映射下面的類時出現問題。AutoMapper支持複雜的類結構嗎?

class LazyStudent : ActiveRecordBase 
{ 
    [Property] 
    public String Name 
    { 
     set; 
     get; 
    } 

    [HasMany(typeof(LazyStudentBook))] 
    public IList<LazyStudentBook> Books 
    { 
     set; 
     get; 
    } 
} 

class LazyStudentBook : ActiveRecordBase 
{ 
    [Property] 
    public String BookName 
    { 
     set; 
     get; 
    } 

    [HasMany(typeof(LazyStudentBookPicture))] 
    public IList<LazyStudentBookPicture> Pictures 
    { 
     set; 
     get; 
    } 

    [BelongsTo] 
    public LazyStudent LazyStudent 
    { 
     set; 
     get; 
    } 
} 

class LazyStudentBookPicture : ActiveRecordBase 
{ 
    [Property] 
    public String PictureName 
    { 
     set; 
     get; 
    } 

    [Property] 
    public LazyStudentBook LazyStudentBook 
    { 
     set; 
     get; 
    } 

} 

class Student 
{ 
    public String Name 
    { 
     set; 
     get; 
    } 
    public IList<StudentBook> Books 
    { 
     set; 
     get; 
    } 
} 

class StudentBook 
{ 
    public String BookName 
    { 
     set; 
     get; 
    } 

    public IList<StudentBookPicture> Pictures 
    { 
     set; 
     get; 
    } 
} 
class StudentBookPicture 
{ 
    public String PictureName 
    { 
     set; 
     get; 
    } 
} 

class TestAutoMapper 
{ 
    public static void Start() 
    { 
     Mapper.CreateMap<LazyStudent, Student>(); 
     Mapper.CreateMap<LazyStudentBook, StudentBook>(); 
     Mapper.CreateMap<LazyStudentBookPicture, StudentBookPicture>(); 
     Mapper.CreateMap<IList<LazyStudent>, IList<LazyStudent>>(); 
     Mapper.CreateMap<IList<LazyStudentBookPicture>, IList<StudentBookPicture>>(); 

     IList<LazyStudent> lazyStudents = new List<LazyStudent> 
     { 
      new LazyStudent{ 
       Name = "AAA", 
       Books = new List<LazyStudentBook>{ 
        new LazyStudentBook{ 
         BookName = "BookName" 
         /*, 
         Pictures = new List<LazyStudentBookPicture>{ 
          new LazyStudentBookPicture{ 
           PictureName = "PictureName" 
          },    new LazyStudentBookPicture{ 
           PictureName = "PictureName" 
          } 
         }*/ 
        } 
       } 
      } 
     }; 

     IList<Student> sList = Mapper.Map<IList<LazyStudent>, IList<Student>>(lazyStudents); 
    } 
} 

上面的代碼工作得很好,但是當我取消「照片=新」 ......這引發此異常

Trying to map System.Collections.Generic.IList`1[[TestAOP.LazyStudent, Test.Com.Ko.Aop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] to System.Collections.Generic.IList`1[[TestAOP.Student, Test.Com.Ko.Aop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]. 
Exception of type 'AutoMapper.AutoMapperMappingException' was thrown. 

劑量人能告訴我什麼是我錯了嗎?
AutoMapper是否支持這個類結構?

謝謝。

+1

我得到了答案:「你並不需要顯式地映射集合類型,只有項目類型」從這個線程http://stackoverflow.com /問題/ 2645293/automapper-未能到地圖上的IEnumerable – wearetherock 2010-07-09 12:26:52

回答

0

這意味着您不必告訴AutoMapper將列表映射到列表。它不關心,只是告訴它如何映射類型。

恩,你算了一下。

以供將來參考,您的創建後,你可以做

AutoMapper.AssertConfigurationIsValid();