Will Entity Framework 6 function normally within Task.Run() statements?

c# entity-framework-6 task-parallel-library

Question

My question is simple, if I perform a read or write within a Task.Run in order to make my method asynchronous, will it work as a normal bit of code would, or is there something within EF that bans this practice?

For example:

await Task.Run(() =>
{
    var data = _context.KittenLog.ToList();
}

I have an uneasy feeling that doing this will open a can of worms, but I can't find anything on google about combining the two.

1
4
2/17/2017 3:35:40 PM

Accepted Answer

Well yes, you could. But there is no need to wrap it in a call to Task.Run since it has native async support, see https://msdn.microsoft.com/en-us/library/jj819165(v=vs.113).aspx#Making it asynchronous

In you case it will become something like:

var data = await _context.KittenLog.ToListAsync(CancellationToken.None);

There a some things to consider. Like the context can only handle one async operation at a time.

5
2/17/2017 4:04:21 PM


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