CS1061 IOrderedEnumerable<TEntity>' 不包含 'ToListAsync' EF Core 6 的定义

CS1061 IOrderedEnumerable<TEntity>' does not contain a definition for 'ToListAsync' EF Core 6

ToListAsync 是否已在 EF 核心 6 上的 EntityFrameWorkCore 中描述?

如果我使用 ToList() 方法,它会起作用,但如果我添加 ToListAsync,我会得到以下错误:

'IOrderedEnumerable' 不包含 'ToListAsync' 的定义,并且找不到可访问的扩展方法 'ToListAsync' 接受类型 'IOrderedEnumerable' 的第一个参数(您是否缺少使用指令或程序集引用?

代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;

namespace RainbowOF.Repositories.Common
{
    public class AppRepository<TEntity> : IAppRepository<TEntity> where TEntity : class
    {
        #region Privates
        private ApplicationDbContext _Context { get; set; }  // = null;
        private DbSet<TEntity> _Table = null;
        private ILoggerManager _Logger { get; }
        private IAppUnitOfWork _AppUnitOfWork { get; set; }
        #endregion
        #region Initialise
        public AppRepository(ApplicationDbContext dbContext, ILoggerManager logger, IAppUnitOfWork unitOfWork)
        {
            _Context = dbContext;
            _Table = dbContext.Set<TEntity>();
            _Logger = logger;
            _AppUnitOfWork = unitOfWork;
        }
        #endregion

        public IEnumerable<TEntity> GetAllOrderBy(Func<TEntity, object> orderByExpression, bool sortDesc = false)
        {
            _Logger.LogDebug($"Getting all records in Table of type: {typeof(TEntity)} order by {orderByExpression}");
            if (_AppUnitOfWork.DBTransactionIsStillRunning())
                _Logger.LogDebug("Second transaction started before current transaction completed!");
            try
            {
                var _result = sortDesc 
                    ? _Table.OrderByDescending(orderByExpression).ToList() 
                    : _Table.OrderBy(orderByExpression).ToList();
                return _result;
            }
            catch (Exception ex)
            {
                _AppUnitOfWork.LogAndSetErrorMessage($"!!!Error Getting all (async): {ex.Message} - Inner Exception {ex.InnerException}");
#if DebugMode
                throw;     // #Debug?
#endif
            }
            return null;
        }

        public async Task<IEnumerable<TEntity>> GetAllAsync()
        {
            _Logger.LogDebug($"Getting all records (async) in Table of type: {typeof(TEntity)}");
            if (_AppUnitOfWork.DBTransactionIsStillRunning())
                _Logger.LogDebug("Second transaction started before current transaction completed!");
            try
            {
                var _result =  await _Table.ToListAsync();
                return _result;
            }
            catch (Exception ex)
            {
                _AppUnitOfWork.LogAndSetErrorMessage($"!!!Error Getting all (async): {ex.Message} - Inner Exception {ex.InnerException}");
#if DebugMode
                throw;     // #Debug?
#endif
            }
            return null;
        }

//--> the above works fine, below gives the error.


        public async Task<IEnumerable<TEntity>> GetAllOrderByAsync(Func<TEntity, object> orderByExpression, bool sortDesc = false)
        {
            _Logger.LogDebug($"Getting all records (async) in Table of type: {typeof(TEntity)} order by {orderByExpression}");
            if (_AppUnitOfWork.DBTransactionIsStillRunning())
                _Logger.LogDebug("Second transaction started before current transaction completed!");
            try
            {
                var _result = sortDesc 
                    ? await _Table.OrderByDescending(orderByExpression).ToListAsync() 
                    : await _Table.OrderBy(orderByExpression).ToListAsync();
                return _result;
            }
            catch (Exception ex)
            {
                _AppUnitOfWork.LogAndSetErrorMessage($"!!!Error Getting all (async) order by: {ex.Message} - Inner Exception {ex.InnerException}");
#if DebugMode
                throw;     // #Debug?
#endif
            }
            return null;
        }

我试过在上面添加。

我Google和几张票说在6中添加了Async,还有一些建议添加“using System.Data.Entity;”到用途。 如果我添加 using System.Data.Entity;然后那个错误消失了,但后来我遇到了与 EntityFrameworkCore 的冲突。

项目已升级到VS2022中的.net 6。它在 .net 5

中工作

可能是我把技术搞糊涂了

OrderBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) which accepts Func is a method defined for IEnumerable which does have async version of ToList. You need OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>)) which is defined for IQueryable which is extended with ToListAsync(你也想要它因为 IQueryable 重载实际上被翻译成 SQL 而 IEnumerable 将把所有数据提取到客户端并对其进行排序在那里)。因此,更改您的方法签名以接受 func (Expression<Func<TEntity, object>>) 的表达式而不仅仅是 func:

public async Task<IEnumerable<TEntity>> GetAllOrderByAsync(Expression<Func<TEntity, object>> orderByExpression, bool sortDesc = false)
{
    ...
}

额外信息:

  • Exrpression vs Func
  • IQueryable vs IEnumerable