我使用Automapper
来映射两个类:
public partial class db_MyObject
{
public int Id { get; set; }
public Nullable<int> ParentId { get; set; }
}
public class MyObject
{
public int Id { get; set; }
public MyObject Parent { get; set; } // Parent can be null
}
如何配置此映射。我试过这个:
Mapper.CreateMap<db_MyObject, MyObject>()
.ForMember(dest => dest.Parent, opt => opt.ResolveUsing(model => new db_MyObject() {
Id = model.ParentId ?? 0 }));
它导致堆栈溢出。
答案 0 :(得分:1)
不应该是
Mapper.CreateMap<db_MyObject, MyObject>()
.ForMember(
dest => dest.Parent,
opt => opt.ResolveUsing(model => new MyObject() {
Id = model.ParentId ?? 0 })
);
即。 new MyObject()
代替new db_MyObject()
,因为Parent
是MyObject
?