对我的问题有一点了解,我有如下四类的employee,employeedto,request和requestedto,它们具有循环引用,我为它们创建了映射配置,但是当我尝试将雇员列表映射到employeedtos时,即使我正在使用MaxDepth(1),也会得到stackoverflow异常。
public class Employee
{
public string Name { get; set; }
public List<Request> Requests { get; set; }
}
public class EmployeeDto
{
public string Name { get; set; }
public List<RequestDto> RequestsDto { get; set; }
}
public class Request
{
public int Id { get; set; }
public Employee CreatedBy { get; set; }
}
public class RequestDto
{
public int Id { get; set; }
public EmployeeDto CreatedBy { get; set; }
}
映射配置:
cfg.CreateMap<Employee, EmployeeDto>().MaxDepth(1)
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
.AfterMap((src, dest, context) =>
{
dest.RequestsDto = context.Mapper.Map<List<RequestDto>>(src.Requests);
});
cfg.CreateMap<EmployeeDto, Employee>().MaxDepth(1)
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
.AfterMap((src, dest, context) =>
{
dest.Requests = context.Mapper.Map<List<Request>>(src.RequestsDto);
});
cfg.CreateMap<Request, RequestDto>().MaxDepth(1)
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
.AfterMap((src, dest, context) =>
{
dest.CreatedBy = context.Mapper.Map<EmployeeDto>(src.CreatedBy);
});
cfg.CreateMap<RequestDto, Request>().MaxDepth(1)
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
.AfterMap((src, dest, context) =>
{
dest.CreatedBy = context.Mapper.Map<Employee>(src.CreatedBy);
});
当我尝试将员工映射到employeetos时,此行发生例外。
var mapped = _mapper.Map<List<EmployeeDto>>(employees);