在F#异步EF查询 [英] Asynchronous EF query in F#

查看:256
本文介绍了在F#异步EF查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中使用EF6我可以轻松地进行异步操作是这样的:

In C# using EF6 I can easily make asynchronous operations like this:

using (var context = new MyDbContext()) {
    var item = await context.SomeEntities.Where(e => e.Id == 1).FirstAsync();
    DoSomething(item);
}

我怎么可以做F#异步工作流程是否相同?

How could I do the same with F# async workflow?

我知道查询的工作流程,以及如何使用它,而不是LINQ的,但我不知道如何使它正常异步(即没有线程池的永久消费像在C#示例)。这是我走到这一步,(它是同步):

I am aware of query workflow and how to use it instead of LINQ, but I have no idea how to make it properly async (i.e. without permanent consumption of thread pool, like in a C# example). Here's what I got so far (it is synchronous):

use context = MyDbContext()
let item = query {
    for e in context.SomeEntities
    where (e.Id = 1)
    head
}
DoSomething item

我要寻找(在C#中的查询类似于 FirstAsync ),如 headAsync 一些操作或其他可靠的解决方案。

I am looking for some operation like headAsync (analogous to FirstAsync in C# query) or other reliable solution.

推荐答案

您可以使用在C#中使用相同的扩展方法。

You can use the same extension methods you use in C#.

open System.Data.Entity

let asyncQuery = async {
    return! 
        query { 
        for e in context.SomeEntities do
        where (e.Id = 1)
        select e}
        |> QueryableExtensions.FirstAsync
        |> Async.AwaitTask
}

let result = Async.RunSynchronously asyncQuery

这篇关于在F#异步EF查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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