What is the purpose of .`ToListAsync()`

.net-core-2.0 c# ef-core-2.0 entity-framework-6


If I try to run this code

await _dbContext.Set<T>().ToListAsync();

I will get this error :

InvalidOperationException: The source IQueryable doesn't implement IDbAsyncEnumerable. Only sources that implement IDbAsyncEnumerable can be used for Entity Framework asynchronous operations. For more details see http://go.microsoft.com/fwlink/?LinkId=287068

By following the provided link we can read the following statement

[...] because they are only designed to be used with Entity Framework you may receive the following error if you try to use them on a LINQ query that isn’t an Entity Framework query.

Then why can't I use it on a DbSet ? To me a DbSet is clearly an entity-framework related class since it is found in the EntityFramework assembly ...

As per the DbSet MSDN documentation, it implements IDbAsyncEnumerable.GetAsyncEnumerator(). If the IDbAsyncEnumerable is implemented, why can't .ToListAsync() run ?

Plus, by digging into the code using the ReSharper decompiler I couldn't understand at which point it goes wrong ... the AsDbAsyncEnumerable method that is internally used tries to cast my DbSet<T> into an IDbAsyncEnumerable<T> which should work since DbSet implements it ...

At this point my code uses the following line, which is exactly what I expected .ToListAsync() was doing internally.

await _dbContext.Set<T>().ToAsyncEnumerable().ToList();

Why isn't ToAsyncEnumerable used internally ? In which case would ToListAsync be used if it doesn't work on either a normal IQueryable nor a DbSet ?

10/17/2017 8:31:45 AM

Accepted Answer

Panagiotis Kanavos and Henk Holterman pointed out the right thing in the comments: EF6 isn't EF Core. For some reason I simply assumed that these two were the same, with "core" being a fancy name used as an alias. It's probably for this reason that I ended up having both in my project.

I tried a lot of things to fix the error but ultimately I fixed everything by simply removing every single package that could have been more or less remotely related to Entity Framework from my project except for Microsoft.NETCore.App because it's mandatory in .netcote 2.0 apps. From there I started adding packages depending on what name-spaces and classes the compiler was crying about using MSDN to find the package's names and it ultimately worked again.

Now to properly answer my question: ToListAsync() does exactly what it's supposed to do. It just doesn't recognise an EF6 DbContext since, it's not part of the EFCore package. So mixing these two up isn't a good idea.

12/1/2017 3:58:05 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow