myesn

myEsn2E9

hi
github

ABP: 使用 EFCore 查询时 Include 导航属性

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));
    }
}

参考#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.