0

I am having an issue with dotnet core. I set a Task to runs on the thread pool with Task.Run (async => await myFunction (). ConfigureAwait (false)). But this specific pice of code finishes after the main thread, and it need to access to the database but is causing a null exception. Can i somehow access the database running this code in second thread?

Thanks!

Lincoln Teixeira
  • 145
  • 2
  • 10
  • You can access the database from any thread, but be aware some databases (like SQL CE) are not reentrant, so changing data from multiple threads can cause race conditions. If you're getting a NullReferenceException you need to [debug your code](https://stackoverflow.com/questions/4660142/). – Dour High Arch Jun 05 '20 at 18:58
  • @DourHighArch thank you. My second thread is calling DbContext of EF but it can no support mult thread, so i need to manage the concurrence. – Lincoln Teixeira Jun 05 '20 at 19:20
  • You can't use the same `DbContext` object on multiple threads, but you can create a new `DbContext` object on each thread. – Gabriel Luci Jun 06 '20 at 23:24
  • @GabrielLuci yes, i guess i will use this one, Thank you! – Lincoln Teixeira Jun 08 '20 at 00:02

1 Answers1

0

The better way i found to fix it, was creating a new DbContext for the background task to avoid conflicts!

Lincoln Teixeira
  • 145
  • 2
  • 10