Search code examples
.netef-core-6.0

EF Core 6: parallelisation in dbContext


In this article: https://learn.microsoft.com/en-us/ef/core/miscellaneous/async Microsoft writes:

EF Core doesn't support multiple parallel operations being run on the same context instance. You should always wait for an operation to complete before beginning the next operation. This is typically done by using the await keyword on each async operation.

Could you please help me to understand: if I have 10_000 users all trying to hit the same ASP.NET end-point, which in turn executes EF Core (LINQ) query (let's say the query is of the form SELECT WHERE <condition> and condition is different for each user).

If EF Core only supports asynchronous but not parallel execution - wouldn't it create a serious latency issue, for one user needs to await for the query to be resolved for the other user?


Solution

  • That only applies to the context (class that derives from DbContext). If you want you run requests in parellel (e.g. for a web api) you are free to create a new context for each request.

    When you have an aspnet project and have used AddDbContext in your startup it'll add the context using dependency injection as scoped service (by default). In other words there's a new context created for each request.