Why DbSet doesn't implement EnumerableAsync

async-await c#-5.0 entity-framework entity-framework-6 ienumerable

Accepted Answer

The layout ofIEnumerable doesn't permit using it withasync /await . IEnumerator<T>.MoveNext() cannot give any backTask object accessible to the callerawait due to the fact that it has a fixed return type ofbool .

The async Version of theIEnumerable is IDbAsyncEnumerable which has already been put into practice byDbQuery<T> through whichDbSet<T> hence, noAsDbAsyncEnumerable() That need the extension method in order to function.

Your Get IMO, version does not require anAsync version. Because it does nothing, it already does not block. The database won't be queried until the caller begins utilizing the returned enumerable. Just a quick modification to the return typeDbQuery<TEntity> . (This needs to be cast, but it ought to already be the kind of concrete that is returned.) The caller can then select between synchronous and asynchronous methods to employ.

(Actually, with closer examination, I realize that despite the fact that your query is concerningDbSet<T> in fact you're using the backupObjectContext as opposed to theDbContext This should provide you withObjectQuery<T> queryables as opposed toDbQuery<T> queryables for which the response will vary. Stop utilizing the and things will be easier for you.ObjectContext except when you absolutely must.)

8/8/2014 1:59:23 PM

Popular Answer

due to the fact that youEnumerable , 8-Zzz-Zzz. You are able toIEnumerable what would your underlying type be in your implementation if List, Array, and Dictionaries all implemented it?ToEnumerableAsync() ?

It is simple to do.

IEnumerable<TEntity> result = await yourDbSet.ToListAsync();

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