问题描述
我使用EF 6.1.1和数据库第一。当我输入一个存储过程到EDMX并生成它看起来像这样的DbContext:
I'm using EF 6.1.1 and Database First. When I import a stored proc into the edmx and generate the DBContext it looks like this:
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<TestSP_Result>("TestSP", params[]...)
这将返回ObjectResult&LT; T>,它实现IDbAsyncEnumerable&LT; T>所以我这样做是为了读取数据异步:
That returns an ObjectResult< T >, which implements IDbAsyncEnumerable< T > so I'm doing this to read the data async:
IDbAsyncEnumerable<T> enumerable = objectResult as IDbAsyncEnumerable<T>;
IDbAsyncEnumerator<T> enumerator = enumerable.GetAsyncEnumerator();
List<T> list = new List<T>();
bool moreItems = await enumerator.MoveNextAsync(CancellationToken.None);
while (moreItems)
{
list.Add(enumerator.Current);
moreItems = await enumerator.MoveNextAsync(CancellationToken.None);
}
return list;
这是真的读取数据不同步?我附上分析器和ExecuteFunction来行实际的SQL语句运行时,不枚举结果时。
Is this really reading the data asynchronously? I attached the profiler and the actual SQL statement runs in the ExecuteFunction line, not when enumerating the results.
有没有要逃避的DbContext一个存储过程并异步读取结果以适当的方式?
Is there a proper way to run a stored proc from the DBContext and read the results asynchronously?
推荐答案
我是怎么做的:
var results = await ctx.Database.SqlQuery<TResult>("EXEC sp_foo {0}, {1}", p1, p2)
.ToArrayAsync();
这篇关于的EntityFramework存储过程,函数导入是它可以读取异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!