EF6 alpha Async Await on an Entity Stored Procedure / Function Import?

一曲冷凌霜 提交于 2019-12-01 05:29:32

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;
}

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.

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.

This is an old thread, but I felt I should share. You should use APM then wrap the synchronous calls in a Task.

Example:

//declare the delegate
private delegate MyResult MySPDelegate();

// declare the synchronous method
private MyResult MySP()
{
    // do work...
}

Then wrap the synchronous method in a Task:

// wraps the method in a task and returns the task.
public Task<MyResult> MySPAsync()
{
    MySPDelegate caller = new MySPDelegate(MySP);
    return Task.Factory.FromAsync(caller.BeginInvoke, caller.EndInvoke, null);
}

Call the async method when you want to execute:

var MyResult = await MySPAsync();

You can use up to three (3) parameters in the methods. Best practice is if you use more than three parameters; you should pass in a class.

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