Make Synchronous database operation asynchronous

266 Views Asked by At

I want to use bulk database operations in Asp.Net MVC 4.5.2 app. There is good lib Z.EntityFramework.Extensions, but it costs 800$. And free lib EF.BulkExtensions, but it doesn't contains async methods. Question: if I will do this trick

await Task.Run(() => 
{ 
    MyDatabaseContext.BulkUpdate(entities); 
});

Will it work the same as if i used the async method (in terms of working with IIS' thread pool ) ?

1

There are 1 best solutions below

0
Oliver On

You should at least do something like:

var task = new Task(() => { MyDatabaseContext.BulkUpdate(entities) }, TaskCreationOptions.LongRunning);
await task;

This ensures, that your task runs in its own thread and doesn't occupy a task from the task pool.