EntityFramework Stored Proc Function Import is it possible to read async?

安稳与你 提交于 2019-12-12 08:43:57

问题


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[]...)

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;

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.

Is there a proper way to run a stored proc from the DBContext and read the results asynchronously?


回答1:


How I do it:

var results = await ctx.Database.SqlQuery<TResult>("EXEC sp_foo {0}, {1}", p1, p2)
                  .ToArrayAsync();


来源:https://stackoverflow.com/questions/25901862/entityframework-stored-proc-function-import-is-it-possible-to-read-async

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!