Getting “The connection does not support MultipleActiveResultSets” in a ForEach with async-await

前端 未结 4 1218
遥遥无期
遥遥无期 2021-01-03 21:42

I have the following code using Dapper.SimpleCRUD :

var test = new FallEnvironmentalCondition[] {
    new FallEnvironmentalCondition {Id=40,FallId=3,Environm         


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-03 22:27

    The problem is the ForEach method is not an asynchronous method. It will not await the Task returned by your lambda. Running that code will fire every task and not wait for completion of any of them.

    General point: marking a lambda as async does not make a synchronous method you pass it into behave asynchronously.

    Solution: you will need to use a foreach loop which awaits the tasks' completion.

    eg: foreach (var x in xs) await f(x);

    You can wrap that in a helper method if you prefer.

    (I know it's an old question, but I don't think it was clearly answered)

提交回复
热议问题