0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[メモ]LINQ WhenAllの罠

Last updated at Posted at 2022-04-05

WhenAllでTaskを呼ぶとキューを順次実行するのではなく同時に並行実行するため、DBの排他制御時など同時実行が不可能な処理を含む場合は注意。
当然実行順序も保証されない。

EFcore参照でawaitしている場合に以下のような例外が発生した。

System.InvalidOperationException: A second operation was started on this context before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913.
   at Microsoft.EntityFrameworkCore.Internal.ConcurrencyDetector.EnterCriticalSection()
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()

なかなかこの情報にたどり着けなかったので個人的な備忘録を残しておく。
横着せず一件ずつ実行してしまえば解決する。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?