当我尝试映射下面的课程时,我遇到了问题。
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);
}
}
上面的代码运行良好,但是当我取消注释“Pictures = new”时...它会抛出此异常
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.
是的,可以告诉我,我错了什么? 谢谢。
答案 0 :(得分:0)
这意味着您不必告诉AutoMapper您将列表映射到列表。它并不关心,只是告诉它如何映射类型。
嗯,你想通了。为了将来参考,在您创建后可以执行
AutoMapper.AssertConfigurationIsValid();