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

前端 未结 2 1613
耶瑟儿~
耶瑟儿~ 2021-01-13 07:13

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.<

2条回答
  •  不要未来只要你来
    2021-01-13 07:47

    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> ToListAsync(this ObjectResult source)
    {
        var list = new List();
        await Task.Run(() => list.AddRange(source.ToList()));
        return list;
    }
    

    If you reflect version 6 of EF you will see that ObjectResult actually implements IDbAsyncEnumerable, IDbAsyncEnumerable. And the method for ToListAsync(this IDbAsyncEnumerable 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(); if you want to return an empty List instead of null.

提交回复
热议问题