EF6 alpha 异步等待实体存储过程/函数导入? [英] EF6 alpha Async Await on an Entity Stored Procedure / Function Import?

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

问题描述

我想将新的异步等待功能应用于在我的实体模型中导入的存储过程/函数导入,但目前还无法使用 EF6 alpha.

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 alpha2(或 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.

我关注了这个(http://entityframework.codeplex.com/wikipage?title=Updating%20Applications%20to%20use%20EF6) 以确保代码引用的是新的 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<T> 实际上实现了 IDbAsyncEnumerable<T>, IDbAsyncEnumerable.ToListAsync<T>(this IDbAsyncEnumerable<T> source) 的方法应该能够像 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.如果要返回空列表而不是 null,可以添加 if (source == null) return new List();.

Edit When 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 alpha 异步等待实体存储过程/函数导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆