本文介绍了EF6阿尔法异步等待上一个实体存储过程/函数导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想申请新的异步等待功能在我的实体模型导入存储过程/函数的进口,但目前还无法与EF6阿尔法。

I'd like to apply the new async await functionality to Stored Procedures / Function Imports imported in my Entity model, but have as yet been unable to with the EF6 alpha.

难道还不可能在EF6的α2(或每晚构建为20211),以调用任何对实体功能导入(它调用存储过程SQL)返回复杂类型集合的新异步方法?例如。

Is it yet possible in EF6 alpha2 (or the nightly build as of 20211) to call any of the new Async methods on an Entity Function Import (which calls a SQL Stored Procedure) that returns a collection of Complex Type? e.g.

private async Task<IList<Company>> getInfo (string id)
{
    using (CustomEntity context = new CustomEntity())
    {
        var query = await context.customStoredProcedure(id).ToListAsync();
        // ".ToListAsync()" method not available on above line

        // OR ALTERNATIVELY
        var query = await (from c in context.customStoredProcedure(id)
                           select new Company
                           {
                              Ident = c.id,
                              Name = c.name,
                              Country = c.country,
                              Sector = c.sector, 
                              etc. etc....
                           }).ToListAsync();
        // ".ToListAsync()" method or any "...Async" methods also not available this way

        return query;
    }
}

ToListAsync,或任何新的异步修改方法似乎并不可用于存储过程/函数进口上述实体;只有标准了ToList或AsNumerable等方法都可以。

"ToListAsync", or any of the new async modified methods do not seem to be available to the above Entity Stored Procedure / Function Import; only the standard "ToList" or "AsNumerable" etc methods are available.

我跟着这个(<一个href=\"http://entityframework.$c$cplex.com/wikipage?title=Updating%20Applications%20to%20use%20EF6\">http://entityframework.$c$cplex.com/wikipage?title=Updating%20Applications%20to%20use%20EF6)以确保code为引用新的EF6 DLL和不EF5,以及更新的各种using语句。除了上面,一切都建立正确的。(.NET框架4.5)

I followed this (http://entityframework.codeplex.com/wikipage?title=Updating%20Applications%20to%20use%20EF6) to make sure the code is referencing the new EF6 dlls and not EF5, as well as updated the various using statements. Aside from above, everything builds correctly. (.NET Framework 4.5)

我唯一一次能看到的异步方法是,如果不是只从数据库中导入存储过程,我也导入表 - 通过实体方面引用该表时,然后按上述(context.SomeTable),一些异步方法出现在智能感知。

The only time I can see the async methods is if instead of only importing stored procedures from the DB, I also import a table--then when referencing that table via the Entity context as above (context.SomeTable), some of the async methods appear in intellisense.

我真的很想开始使用新的异步等待上之前返回数据作为JSON多个存储过程的功能,但一直没能得到它的工作至今。

I'd really like to start using the new async await functionality on multiple Stored Procedures prior to returning data as JSON, but have not been able to get it to work so far.

我是不是做错了什么?是异步功能无法在实体存储过程/函数的进口?谢谢你的建议。

Am I doing something wrong? Is async functionality not possible on Entity stored procedure / function imports? Thanks for your advice.

推荐答案

现在这绝不是最佳的解决方案。我增加了一个扩展方法,这样我可以打电话给我的存储过程的await。在EF6.1 +的新版本,我们应该看到这起正式实施。在此之前的虚拟扩展方法做这项工作。

Now this is by no means the best solution. I added an extension method so that I could call await on my stored procedures. In the newer releases of EF6.1+ we should see this officially implemented. Until then a dummy extension method does the job.

static async Task<List<T>> ToListAsync<T>(this ObjectResult<T> source)
{
    var list = new List<T>();
    await Task.Run(() => list.AddRange(source.ToList()));
    return list;
}

如果你反映EF的第6版,您将看到 ObjectResult&LT; T&GT; 真正实现 IDbAsyncEnumerable&LT; T&GT ;, IDbAsyncEnumerable 。和 ToListAsync项&lt; T&GT;(这IDbAsyncEnumerable&LT; T&GT;源)。应该能够连线它一样的LINQ查询

If you reflect version 6 of EF you will see that ObjectResult<T> actually implements IDbAsyncEnumerable<T>, IDbAsyncEnumerable. And the method for ToListAsync<T>(this IDbAsyncEnumerable<T> source) should be able to wire it up the same as a LINQ query.

修改
当ObjectResult。返回空空。你可以添加如果(来源== NULL)返回新的List&LT; T&GT;(); 如果你想,而不是返回空的空List

EditWhen the ObjectResult is empty null is returned. You could add if (source == null) return new List<T>(); if you want to return an empty List instead of null.

这篇关于EF6阿尔法异步等待上一个实体存储过程/函数导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 00:57