问题描述
在实体框架6.1.1一个IDbSet重新presents其可以从数据库中查询和它的具体实现是DbSet中描述的实体的集合
In Entity framework 6.1.1 an IDbSet represents the collection of entities which can be queried from the database and its concrete implementation is DbSet as described in
为什么这个界面还是它的具体实现不包含ToEnumerableAsync或AsEnumerableAsync但ToListAsync,ToArrayAsync任何定义,ToDictionaryAsync?
How come this interface or its concrete implementation doesn't contain any definition for ToEnumerableAsync or AsEnumerableAsync but ToListAsync,ToArrayAsync,ToDictionaryAsync?
要给你,为什么我碰到这个问题,我有以下的一段code的,我想使异步来了一个想法:
To give you an idea of why I came across this question I have the following piece of code which I wanted to make async:
public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
{
string entityName = GetEntityName<TEntity>();
return ((IObjectContextAdapter)DbContext).ObjectContext.CreateQuery<TEntity>(entityName);
}
public IEnumerable<TEntity> Get<TEntity, TOrderBy>(Expression<Func<TEntity, TOrderBy>> orderBy, int pageIndex, int pageSize, SortOrder sortOrder = SortOrder.Ascending) where TEntity : class
{
if (sortOrder == SortOrder.Ascending)
{
return GetQuery<TEntity>().OrderBy(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).AsEnumerable();
}
return
GetQuery<TEntity>()
.OrderByDescending(orderBy)
.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.AsEnumerable();
}
里面说到我心中唯一的实现如下:
The only implementation which comes to my mind is as follows:
public async Task<IEnumerable<TEntity>> GetAsync<TEntity, TOrderBy>(Expression<Func<TEntity, TOrderBy>> orderBy, int pageIndex, int pageSize, SortOrder sortOrder = SortOrder.Ascending) where TEntity : class
{
if (sortOrder == SortOrder.Ascending)
{
return await GetQuery<TEntity>().OrderBy(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
}
return
await GetQuery<TEntity>()
.OrderByDescending(orderBy)
.Skip((pageIndex - 1) * pageSize)
.Take(pageSize)
.ToListAsync();
}
这是对于正确的方法,使一个异步方法?
Is this the correct approach in regards to making a method asynchronous?
以上方法都属于在以下环节实现了通用仓库:
要深入挖掘你可以看一下原始博客:
<一href=\"http://huyrua.word$p$pss.com/2010/07/13/entity-framework-4-poco-repository-and-specification-pattern/\">Entity框架POCO,库和规范模式
Above methods belong to the generic repository implemented in the following links:Entity Framework POCO, Repository and Specification PatternTo dig deeper you can have a look at the originating blog:Entity Framework POCO, Repository and Specification Pattern
推荐答案
的IEnumerable
的设计不允许它与异步使用
/ 等待
。 的IEnumerator&LT; T&GT; .MoveNext()
无法返回任何工作
对象,调用者可以等待
,因为它已经得到了一个固定的返回类型布尔
。
The design of IEnumerable
doesn't allow it to be used with async
/await
. IEnumerator<T>.MoveNext()
cannot return any Task
object that the caller can await
, because it has got a fixed return type of bool
.
的IEnumerable
的异步
-Aware版本是 IDbAsyncEnumerable
,从 DbSet&LT; T&GT;
派生,所以没有<$>,而这已经被的DBQuery&LT实施C $ C> AsDbAsyncEnumerable()
扩展方法是必要的工作。
The async
-aware version of IEnumerable
is IDbAsyncEnumerable
, and that's already implemented by DbQuery<T>
, from which DbSet<T>
derives, so no AsDbAsyncEnumerable()
extension method is necessary for that to work.
您获取
版本,国际海事组织,并不需要一个异步
版本。它已不阻止,因为它没有做任何事情。只有当主叫用户开始使用返回的枚举,将在数据库中查询。我只是改变返回类型为的DBQuery&LT; TEntity&GT;
。 (这需要一个铸造的,但应已被返回的具体类型。)然后,主叫方可以决定是否使用同步方法或异步方法
Your Get
version, IMO, does not need an Async
version. It already doesn't block, because it doesn't do anything. Only when the caller starts using the returned enumerable, will the database be queried. I'd just change the return type to DbQuery<TEntity>
. (This requires a cast, but should already be the concrete type that gets returned.) The caller can then decide whether to use synchronous methods, or asynchronous methods.
(其实,仔细观察,我看到,虽然你的问题是关于 DbSet&LT; T&GT;
,你实际使用备份的ObjectContext
而不是的DbContext
这将可能给你的ObjectQuery&LT; T&GT;
queryables而不是的DBQuery&LT; T&GT;
queryables,对于这个问题的答案将是不同的你会让事情对自己更容易,如果你停止使用的ObjectContext
当你的真的除了的需要。)
(Actually, on closer inspection, I see that although your question is about DbSet<T>
, you're actually using the backing ObjectContext
instead of the DbContext
. This will likely give you ObjectQuery<T>
queryables rather than DbQuery<T>
queryables, for which the answer will be different. You'll make things easier on yourself if you stop using the ObjectContext
except when you really need to.)
这篇关于为什么DbSet&LT; TEntity&GT;没有实现EnumerableAsync的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!