EntityFramework ToListAsync() does not work
13,287
I suspect that further up your call stack, your code is calling Task.Wait
or Task<T>.Result
. If you do this on the UI thread or from an ASP.NET request context, your code will deadlock, as I explain on my blog.
To fix it, use await
instead of Task.Wait
or Task<T>.Result
.
Author by
Martin Majoroš
Updated on July 23, 2022Comments
-
Martin Majoroš almost 2 years
I try to call EF method ToListAsync. But nothing happend - no exception, no timeout just running.
This is my code.
private IQueryable<Place> placeCompleteQuery; protected IQueryable<Place> PlaceCompleteQuery { get { return this.placeCompleteQuery ?? (this.placeCompleteQuery = this.Context.Places.Include(p => p.Address). Include(p => p.CreatedBy). Include(p => p.Source). Include(p => p.Type.Translations). Include(p => p.Ratings)); } } public async Task<IList<Place>> GetPlacesByLocationAsync(DbGeography location, int radius) { List<Place> temporaryResult = PlaceCompleteQuery.Where(p => p.Location.Distance(location) <= radius). ToList(); return await PlaceCompleteQuery.Where(p => p.Location.Distance(location) <= radius). ToListAsync(); }
The first sync call of ToList method return result immediately. The second async call of ToListAsync still running with no result nor exception.
Any suggestions?