Entity Framework Core: different threads using the same instance of DbContext

asp.net-core entity-framework entity-framework-core multithreading

Accepted Answer

You're attempting to utilize a single, shared resource.Project class) to carry out concurrent actions (saveUserLog The implementation of your shared resources is not thread-safe (exceptions raised).

There are three possible solutions at least:

  1. Register before using a shared resource.Project instead of singletone, per scope;
  2. Avoid running tasks concurrently: this looks difficult to do since you're creating a web application and you can't make users wait.
  3. Make your resource thread-safe by refactoring it: add mutexes, locks, and buffering withinProject

There isn't just one "right" technique; all three are legitimate. Pick one you like (or combine several).

Using scoped dbcontext is advised (due to connections being pooled), however the choice should be made by the app's developer.

1
11/11/2019 1:31:34 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