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



ToListAsync是否已在EF核心6上的EntityFrameWorkCore中取消验证?

如果我使用ToList((方法,它是有效的,但如果我添加ToListAsync,我会得到以下错误:

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

代码示例:

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

我试着在上面添加。

我在谷歌上搜索了几张票,说异步是在6中添加的,有些人建议添加"异步";使用System.Data.Entity"使用。如果我使用System.Data.Entity;然后这个错误就消失了,但后来我与EntityFrameworkCore发生了冲突。

该项目已在VS2022中升级为.net 6。它在.net 5 中工作

也许我混淆了技术。

接受FuncOrderBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)是为具有异步版本ToListIEnumerable定义的方法。您需要为使用ToListAsync扩展的IQueryable定义的OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>))(您也需要它,因为IQueryable重载实际上被转换为SQL,而IEnumerable重载将把所有数据提取到客户端并在那里排序(。因此,更改您的方法签名以接受func(Expression<Func<TEntity, object>>(的表达式,而不仅仅是func:
public async Task<IEnumerable<TEntity>> GetAllOrderByAsync(Expression<Func<TEntity, object>> orderByExpression, bool sortDesc = false)
{
...
}

额外信息:

  • Exrpression与Func
  • IQueryable与IEnumerable

最新更新