开发者

Does AutoMapper Supported Complex Class Structure?

开发者 https://www.devze.com 2023-01-06 02:33 出处:网络
I have a problem when I try to map following class. class LazyStudent : ActiveRecordBase { [Property] public String Name

I have a problem when I try to map following class.

 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);
    }
}

Above code is work well, but when I uncomment "Pictures = new"... It throws this exception

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.

Dose one can tell me What is my wrong?

Is AutoMapper supported this class stucture?

Thank you.


It means you don't have to tell AutoMapper you're mapping a list to a list. It doesn't care, just tell it how to map types.

Nm, you figured it.

For future reference, after your creates you can do

AutoMapper.AssertConfigurationIsValid();
0

精彩评论

暂无评论...
验证码 换一张
取 消