EntityFramework Stored Proc Function Import 是否可以异步读取?

EntityFramework Stored Proc Function Import is it possible to read async?(EntityFramework Stored Proc Function Import 是否可以异步读取?)
本文介绍了EntityFramework Stored Proc Function Import 是否可以异步读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 EF 6.1.1 和 Database First.当我将存储过程导入 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

T>,实现IDbAsyncEnumerable 所以我这样做是为了异步读取数据:

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;

这真的是异步读取数据吗?我附加了分析器,实际的 SQL 语句在 ExecuteFunction 行中运行,而不是在枚举结果时.

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 Stored Proc Function Import 是否可以异步读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)