Aspnetboilerplate应用程序在对ISoftDelete实体的查询上应用排序时给出运行时错误

问题描述 投票:0回答:1

我想为名为Location的实体编写CRUD服务。位置具有复合主键,因此我无法使用AsyncCrudAppService并从AsyncCrudAppService复制所需的方法。

当我使用不带ApplySorting方法的GetAll服务时,它可以正常工作。但是,当我添加排序时,出现此运行时错误:

[[35]错误,BookingSystem.middlewares.HttpGlobalExceptionFilter [(null)]-LINQ表达式'DbSet.Where(l => __ef_filter__p_0 ||!((((ISoftDelete)l).IsDeleted)).OrderByDescending(l => l.Id)'无法翻译。可以以可翻译的形式重写查询,也可以通过插入对AsEnumerable(),AsAsyncEnumerable(),ToList()或ToListAsync()的调用来显式切换到客户端评估。小号ee https://go.microsoft.com/fwlink/?linkid=2101038了解更多信息。

public class Location : IEntity<int>, IPassivable, IFullAudited<User> {
    public int Id { get; set; }
    public int IdLocation { get; set; }         //primary key 0
    public LocationType TypeId { get; set; }    //primary key 1
    public string Name { get; set; }
}

public interface ILocationService : IApplicationService
{
    Task<PagedResultDto<LocationDto>> GetAllAsync(PagedLocationResultRequestDto input);
}

public class LocationService : AbpServiceBase, ILocationService, IPerWebRequestDependency
{
    private readonly IRepository<Location, int> _repository;
    private IAsyncQueryableExecuter AsyncQueryableExecuter { get; set; }

    public LocationService(IRepository<Location> repository)
    {
        _repository = repository;
        AsyncQueryableExecuter = NullAsyncQueryableExecuter.Instance;
    }


    public async Task<PagedResultDto<LocationDto>> GetAllAsync(PagedLocationResultRequestDto input)
    {
        if (input.MaxResultCount > _appSettings.Value.MaxResultCount)
        {
            throw new BookingSystemUserFriendlyException(BookingSystemExceptionCode.InputNotValid,
                nameof(input.MaxResultCount));
        }

        var query = CreateFilteredQuery(input);

        var totalCount = await AsyncQueryableExecuter.CountAsync(query);

        query = ApplySorting(query, input);
        query = ApplyPaging(query, input);

        var entities = await AsyncQueryableExecuter.ToListAsync(query);

        return new PagedResultDto<LocationDto>(
            totalCount,
            entities.Select(MapToEntityDto).ToList()
        );
    }

    protected virtual IQueryable<Location> ApplySorting(
        IQueryable<Location> query, PagedLocationResultRequestDto input)
    {
        if (input is ISortedResultRequest sortInput &&
            !sortInput.Sorting.IsNullOrWhiteSpace())
        {
            return query.OrderBy(sortInput.Sorting);
        }

        return query.OrderByDescending(e => e.Id);
    }

    protected virtual IQueryable<Location> ApplyPaging(
        IQueryable<Location> query, PagedLocationResultRequestDto input)
    {
        if (input is IPagedResultRequest pagedInput)
        {
            return query.PageBy(pagedInput);
        }

        return query;
    }

    private IQueryable<Location> CreateFilteredQuery(PagedLocationResultRequestDto input)
    {
        return _repository.GetAll()
            .WhereIf(!string.IsNullOrWhiteSpace(input.Name),
                location => location.Name.ToLower().Contains(input.Name.Trim().ToLower()));
    }

    private LocationDto MapToEntityDto(Location entity)
    {
        return ObjectMapper.Map<LocationDto>(entity);
    }
}

Abp软件包版本:5.1.0

基础框架:.Net核心

asp.net entity-framework aspnetboilerplate
1个回答
0
投票

嗯,我在项目的GitHub上问了同样的问题,并得到了答案。在ApplySorting中,默认排序基于Id,在我的数据库表中不存在。

如果使用复合PK,则数据库中可能没有Id字段,对吗?然后,您不应该按ID排序。

https://github.com/aspnetboilerplate/aspnetboilerplate/issues/5274#issuecomment-583946065

© www.soinside.com 2019 - 2024. All rights reserved.