class MyService : CrudAppService {}
比如在调用 MyService.GetListAsync 时 Include 导航属性,那么就只需要重写 GetListAsync
默认实现内部调用的 CreateFilteredQueryAsync
,它这个函数中获取 IQueryable<Entity>
,我们重写一下:
protected override async Task<IQueryable<ReportPartTwo>> CreateFilteredQueryAsync(ReportPartTwoGetListRequestDto input)
{
return await ReadOnlyRepository.WithDetailsAsync(x => x.Customer);
//return await ReadOnlyRepository.GetQueryableAsync();
}
在 WithDetailsAsync
函数的 lambda 表达式中包含导航属性,如果是多个,可能写法是 new { x.A, x.B }
。
Include 之后,需要修改我们的 XDto,然后再修改 Mapper 映射,这样就能从 GetListAsync 的输出中拿到导航属性对象中的值了,比如:
using AutoMapper;
using X.Dtos;
namespace X;
public class XApplicationAutoMapperProfile : Profile
{
public XApplicationAutoMapperProfile()
{
/* You can configure your AutoMapper mapping configuration here.
* Alternatively, you can split your mapping configurations
* into multiple profile classes for a better organization. */
CreateMap<X, XDto>()
.ForMember(dest => dest.CustomerName, opt => opt.MapFrom(src => src.Customer.Name));
}
}
参考#
- https://stackoverflow.com/a/64629710/7712266
- https://docs.abp.io/en/abp/latest/Best-Practices/Entity-Framework-Core-Integration
- https://stackoverflow.com/questions/42135149/ef-core-no-include-method-on-dbset
- https://github.com/abpframework/abp/issues/2474
- https://docs.abp.io/zh-Hans/abp/latest/Best-Practices/Entity-Framework-Core-Integration
- https://docs.abp.io/en/abp/latest/Entity-Framework-Core