Aspnetboilerplate 应用程序在对 ISoftDelete 实体的查询应用排序时出现运行时错误
Aspnetboilerplate application gives runtime error while applying sort on a query for ISoftDelete entity
我想为名为 Location 的实体编写 CRUD 服务。 Location 有一个复合主键,所以我无法使用 AsyncCrudAppService 并从 AsyncCrudAppService 复制了我想要的方法。
当我在没有 ApplySorting 方法的情况下使用 GetAll 服务时,它工作正常。但是当我添加排序时,我得到这个运行时错误:
[35] ERROR BookingSystem.middlewares.HttpGlobalExceptionFilter [(null)] - The LINQ expression 'DbSet
.Where(l => __ef_filter__p_0 || !(((ISoftDelete)l).IsDeleted))
.OrderByDescending(l => l.Id)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). S
ee https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
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 Core
好吧,我在项目的 GitHub 中问了同样的问题并得到了答案。
在 ApplySorting 中,默认排序基于 Id,我的数据库 table.
中不存在该排序
If you are using composite PK, then you probably don't have the Id field in the database, right? Then you should not sort by Id.
https://github.com/aspnetboilerplate/aspnetboilerplate/issues/5274#issuecomment-583946065
我想为名为 Location 的实体编写 CRUD 服务。 Location 有一个复合主键,所以我无法使用 AsyncCrudAppService 并从 AsyncCrudAppService 复制了我想要的方法。
当我在没有 ApplySorting 方法的情况下使用 GetAll 服务时,它工作正常。但是当我添加排序时,我得到这个运行时错误:
[35] ERROR BookingSystem.middlewares.HttpGlobalExceptionFilter [(null)] - The LINQ expression 'DbSet .Where(l => __ef_filter__p_0 || !(((ISoftDelete)l).IsDeleted)) .OrderByDescending(l => l.Id)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). S ee https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
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 Core
好吧,我在项目的 GitHub 中问了同样的问题并得到了答案。 在 ApplySorting 中,默认排序基于 Id,我的数据库 table.
中不存在该排序If you are using composite PK, then you probably don't have the Id field in the database, right? Then you should not sort by Id.
https://github.com/aspnetboilerplate/aspnetboilerplate/issues/5274#issuecomment-583946065