ObjectContext ExecuteFunction with async task

asp.net-web-api asynchronous entity-framework-6 task

Question

Using Entity Framework 6 with asp.net webapi, and map with stored procedure, how to convert an ObjectContext execution method to an async task ?

1
0
1/19/2017 11:21:46 AM

Popular Answer

As an alternative to the ExecuteFunction you could use the following construct which will provide you with asynchronous execution:

Task<SomeResult[]> results = ctx.Database.SqlQuery<SomeResult>("EXEC sp_myproc {0}, {1}, {2}", p1, p2, p3).ToArrayAsync();

or:

SomeResult[] results = await ctx.Database.SqlQuery<SomeResult>("EXEC sp_myproc {0}, {1}, {2}", p1, p2, p3).ToArrayAsync();

If you need to use the ExecuteFunction method you could try this:

IDbAsyncEnumerable<SomeResult> enumerable = this.ExecuteFunction<SomeResult>("sp_myproc", p1, p2, p3) as IDbAsyncEnumerable<SomeResult>;
IDbAsyncEnumerator<SomeResult> enumerator = enumerable.GetAsyncEnumerator();

while (await enumerator.MoveNextAsync(CancellationToken.None))
{
    SomeResult res = enumerator.Current;
    ...
}

This being said, it is worth mentioning that if you decide to use the ExecuteFunction method the actual SQL statement will run on the ExecuteFunction line, and not when enumerating the results.

1
1/13/2017 8:55:14 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow