我需要C#中第二层嵌套自动映射器的帮助。
我的课程如下:
public class Event
{
int EventId { get; set; }
int EventName { get; set; },
Student Student { get; set; }
}
public class Student
{
int StudentId { get; set; }
int StudentName { get; set; },
Address Address { get; set; },
}
public class Address
{
string AddressName { get; set; }
string City { get; set; }
}
public class SourceEvent
{
int EventId { get; set; }
int EventName { get; set; },
int StudentId { get; set; }
int StudentName { get; set; },
string AddressName { get; set; }
string City { get; set; },
}
cfg.CreateMap<SourceEvent, Event>()
.ForMember(d => d.Student,
o => o.MapFrom(
s => new Student
{
StudentId = s.StudentId,
StudentName = s.StudentName,
}
)
);
});
如何使用学生和地址将配置从SourceEvent自动映射到事件?
我知道如何映射第一级,但是不确定如何映射第二级(地址类)
答案 0 :(得分:0)
我在下面找到了自己的答案来添加地址类别。
cfg.CreateMap<SourceEvent, Event>()
.ForMember(d => d.Student,
o => o.MapFrom(
s => new Student
{
StudentId = s.StudentId,
StudentName = s.StudentName,
Address = new Address {
AddressName = s.AddressName,
City = s.City
}
}));
});