How to handle database operations using threadpool infrastructure and entities?

I really need code examples here ... What I'm trying to do:

GetCollectionFromDatabase
foreach item
{
    resetEvents[i] = new ManualResetEvent(false);
    var makeRequest = new MakeRequest(resetEvents[i]);

    ThreadPool.QueueUserWorkItem(new WaitCallback(makeRequest.ThreadPoolCallback), i);
}
db.Save();

ThreadPoolCallback    
{
    update/delete the row 
}

Is this the right thing to do? I pass in db as a link to ThreadPoolCallBack?

+3
source share
1 answer

You will need to create a context in the thread pool callback. Entity structure contexts are not multithreaded and cannot be used by different threads.

I believe that dbis your database context in this case. You will need to create a new one in ThreadPoolCallbackand save it there.

+1
source

Source: https://habr.com/ru/post/1771892/


All Articles