实体框架取消长时间运行的查询 [英] entity framework cancel long running query

查看:136
本文介绍了实体框架取消长时间运行的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的TPL。我使用的是第三方物流做一些异步调用数据库。下面GetDocumentAsync方法被调用多次,并在卸载不同的线程任务,以保持UI线程响应做好。

有两个目标位置:1)保持UI线程响应2)使用户能够中止请求的能力。

我设法中止请求,但是我不能放弃的实体框架已经投入到数据库的请求和查询的数据库级别上运行..或者也许还没有开始。

因此​​,GetDocuments方法仍然返回的取消任务的文件。

有没有走,我可以中止从EF的要求?我可以做任何事情,我实现更好??

 实体_context =新的实体();    CancellationTokenSource _tokenSource =新CancellationTokenSource();    公共异步无效GetDocumentsAsync(用户名字符串)
    {
        IList的<文件>结果;
        尝试
        {
            结果=等待
            任务<列表与lt;文件>> .Factory.StartNew(()=>
            {
                _tokenSource.Token.ThrowIfCancellationRequested();
                返回GetDocuments(用户名);
            },_tokenSource);        }
        赶上(OperationCanceledException前)
        {
            的Debug.WriteLine(的String.Format(任务取消用户{0}上线,用户名));
        }        如果(!_ tokenSource.IsCancellationRequested)
        {
            //结果被用于更新UI
        }
    }    公共无效中止()
    {
        _tokenSource.Cancel();
    }    公开名单<文件> GetDocuments(用户名字符串)
    {
        //我使用的连接模式,需要使用更改跟踪和其他物品的上下文..
        VAR查询从C在_context.Documents =
                    其中,c.CreatedBy userName的==
                    选择C;        查询= query.Take(50); //我希望能够取消这个查询。可以这样做?        返回query.ToList();
    }


解决方案

异步支持即将推出的EF6的一部分。

查看KSA的相关博客文章的概述。

http://odeto$c$c.com/Blogs/scott/archive/2012/08/26/async-in-entity-framework-6-0.aspx

使用,你会切换到ToListAsync一个取消标记。

<一个href=\"http://entityframework.$c$cplex.com/SourceControl/changeset/view/fe17fe748307#src%2fEntityFramework%2fIQueryableExtensions.cs\" rel=\"nofollow\">http://entityframework.$c$cplex.com/SourceControl/changeset/view/fe17fe748307#src%2fEntityFramework%2fIQueryableExtensions.cs

I am new to the TPL. I am using the TPL to make some async calls to the database. Below the GetDocumentAsync method is called multiple times and do a good job at offloading the task on a different thread to keep the UI thread responsive.

There are two objectives here: 1) Keep the UI Thread Responsive 2) Give the user the ability to abort the request.

I have managed to abort the request however i am unable to abort the request that Entity framework has already put to the database and the query is running at the db level.. or perhaps it has not even started.

Therefore the GetDocuments method still returns the documents on the canceled tasks.

Is there a away i can abort the request from the EF?? Can i do anything better in my implementation ??

    Entities _context = new Entities();

    CancellationTokenSource _tokenSource = new CancellationTokenSource();

    public async void GetDocumentsAsync(string userName)
    {
        IList<Document> results;
        try
        {
            results = await 
            Task<List<Document>>.Factory.StartNew(() =>
            {
                _tokenSource.Token.ThrowIfCancellationRequested();
                return GetDocuments(userName);
            }, _tokenSource);

        }
        catch (OperationCanceledException ex)
        {
            Debug.WriteLine(string.Format("Task canceled for user {0} on thread", userName ));
        }

        if(!_tokenSource.IsCancellationRequested)
        {
            // results is used to update the UI 
        }
    }

    public void Abort()
    {
        _tokenSource.Cancel();
    }

    public List<Document> GetDocuments(string userName)
    {
        //I am using the connected model and need to use the Context for change tracking and other goodies..
        var query = from c in _context.Documents
                    where c.CreatedBy == userName
                    select c;

        query = query.Take(50); // I want to be able to cancel this query. Can this be done ???

        return query.ToList();
    }

解决方案

Async support is part of the upcoming EF6.

Check out KSA's related blog post for an overview.

http://odetocode.com/Blogs/scott/archive/2012/08/26/async-in-entity-framework-6-0.aspx

With that, you'll switch to ToListAsync with a cancellation token.

http://entityframework.codeplex.com/SourceControl/changeset/view/fe17fe748307#src%2fEntityFramework%2fIQueryableExtensions.cs

这篇关于实体框架取消长时间运行的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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