Automapper是否可以使用实体';的导航属性从相关实体中提取值
本文关键字:实体 属性 导航 提取 可以使 是否 Automapper | 更新日期: 2023-09-27 18:29:59
Automapper是否能够深入实体的导航属性以映射到DTO类?以下是我正在做的将实体框架查询的结果映射到DTO:的操作
public List<ProductRequestDetailDto> GetProductRequestExtendedDetailAll()
{
List<ProductRequest> aProductRequestList = unitOfWork.getProductRequestRepository().GetProductRequestExtendedDetailAll();
List<ProductRequestDetailDto> ProductRequestDetailDtoList = new List<ProductRequestDetailDto>();
foreach (ProductRequest Req in aProductRequestList)
{
ProductRequestDetailDto ProdReqDetDto = new ProductRequestDetailDto();
ProdReqDetDto.ProductRequestId = Req.ProductRequestId;
ProdReqDetDto.FirstName = Req.Employee.FirstName;
ProdReqDetDto.MiddleInitial = Req.Employee.MiddleInitial;
ProdReqDetDto.LastName = Req.Employee.LastName;
ProdReqDetDto.DeptName = Req.Employee.Department.DeptName;
ProdReqDetDto.DeviceType = Req.ProductProfile.DeviceType;
ProdReqDetDto.ProductName = Req.ProductProfile.ProductName;
ProdReqDetDto.ProductId = Req.ProductProfile.ProductId;
ProdReqDetDto.ProductRequestStageId = Req.ProductRequestStage.ProductRequestStageId;
ProdReqDetDto.DateRequested = Req.DateRequested;
ProdReqDetDto.DateCompleted = Req.DateCompleted;
ProdReqDetDto.SerialNumber = Req.SerialNumber;
ProdReqDetDto.PhoneNumber = Req.PhoneNumber;
ProductRequestDetailDtoList.Add(ProdReqDetDto);
}
return ProductRequestDetailDtoList;
public List<ProductRequest> GetProductRequestExtendedDetailAll()
{
var ReportResult = from Req in context.ProductRequests
select Req;
return ReportResult.ToList();
}
如果Automapper能帮我做到这一点,我希望避免做上述操作。当我不需要深入到一个实体的导航属性,而该属性会导致其他实体时,Automapper已经能够将结果映射到我的DTO。我尝试了以下操作,但没有成功,可能是因为我需要导航到其他实体(如Employee、Department和ProductProfile)的信息:
List<ProductRequestDetailDto> ProductRequestDetailDtoList = Mapper.Map<List<ProductRequestDetailDto>>(aProductRequestList);
如果可以做到这一点,正确的方法是什么?
如果你查看了自动映射器的扩展Queryable Extensions,它有一个扩展。ProjectTo可能会帮助你实现你想要实现的目标,否则你需要为给定的情况创建一个映射配置。
像
AutoMapper.Mapper.CreateMap<ProductRequestDetailDto, Req>()
.ForMember(dest => dest.FirstName ,
opts => opts.MapFrom(src => src.Employee.FirstName));
不,它无法深入到属性中,因为它不知道应该有多深,也不知道如何处理歧义。
默认情况下,它只自动映射具有相同名称的属性,因此这已经为您节省了一些代码,但您仍然需要教它如何映射其他属性。它还可以将一个列表映射到另一个列表,如果它们所持有的类型彼此之间有映射(这将消除对foreach
循环的需要)。
请注意,如果具有相同名称的属性没有相同的类型,则还需要为它们添加映射(如果它们不能相互转换)。
public List<ProductRequestDetailDto> GetProductRequestExtendedDetailAll()
{
AutoMapper.Mapper.CreateMap<ProductRequest, ProductRequestDetailDto>()
.ForMember(dest => dest.FirstName, opt => opt.MapFrom(src => src.Employee.FirstName))
.ForMember(dest => dest.MiddleInitial, opt => opt.MapFrom(src => src.Employee.MiddleInitial))
.ForMember(dest => dest.LastName, opt => opt.MapFrom(src => src.Employee.LastName))
.ForMember(dest => dest.DeptName, opt => opt.MapFrom(src => src.Employee.Department.DeptName))
.ForMember(dest => dest.DeviceType, opt => opt.MapFrom(src => src.ProductProfile.DeviceType))
.ForMember(dest => dest.ProductName, opt => opt.MapFrom(src => src.ProductProfile.ProductName))
.ForMember(dest => dest.ProductId, opt => opt.MapFrom(src => src.ProductProfile.ProductId))
.ForMember(dest => dest.ProductRequestStageId, opt => opt.MapFrom(src => src.ProductRequestStage.ProductRequestStageId));
IQueryable<ProductRequest> aProductRequestList = unitOfWork.getProductRequestRepository().GetProductRequestExtendedDetailAll();
List<ProductRequestDetailDto> ProductRequestDetailDtoList = aProductRequestList.ProjectTo<ProductRequestDetailDto>().ToList();
// or also
// List<ProductRequestDetailDto> ProductRequestDetailDtoList = aProductRequestList.Select(AutoMapper.Mapper.Map<ProductRequestDetailDto>).ToList();
return ProductRequestDetailDtoList;
}
public IQueryable<ProductRequest> GetProductRequestExtendedDetailAll()
{
var ReportResult = from Req in context.ProductRequests
select Req;
return ReportResult;
}