Has ToListAsync been depricated in EntityFrameWorkCore on EF core 6?
If I use the ToList() method it works, but if I add ToListAsync I get the following erro:
'IOrderedEnumerable' does not contain a definition for 'ToListAsync' and no accessible extension method 'ToListAsync' accepting a first argument of type 'IOrderedEnumerable' could be found (are you missing a using directive or an assembly reference?
Sample of the code:
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;
}
I have tried adding in the above.
I Google and a few tickets said Async is added in 6, and some suggested adding "using System.Data.Entity;" to the usings. If I add using System.Data.Entity; then that error goes away, but then I get conflicts with EntityFrameworkCore.
The project has been upgraded to .net 6 in VS2022. It was working in .net 5
Perhaps I am confusing technologies.
OrderBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)
which accepts Func
is a method defined for IEnumerable
which does not 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
(you also want it because IQueryable
overloads are actually translated into SQL while IEnumerable
ones will fetch all the data to the client side and sort it on there). So change your method signature to accept expression of func (Expression<Func<TEntity, object>>
) not just func:
public async Task<IEnumerable<TEntity>> GetAllOrderByAsync(Expression<Func<TEntity, object>> orderByExpression, bool sortDesc = false)
{
...
}
Extra info: