A second operation started on this context before a previous asynchronous operation completed

后端 未结 1 424
不知归路
不知归路 2021-02-05 08:54

Message:

\"System.NotSupportedException was unhandled
Message: An unhandled exception of type \'System.NotSupportedException\' occurred in mscor         


        
1条回答
  •  礼貌的吻别
    2021-02-05 09:25

    Here's your problem:

    userLangs.ForEach(async
    

    This is creating an async void method, because ForEach does not understand asynchronous delegates. So the body of the ForEach will be run concurrently, and Entity Framework does not support concurrent asynchronous access.

    Change the ForEach to a foreach, and you should be good:

    foreach (var l in userLangs)
    {
      var userLanguage = new UserLang();
      userLanguage.UserId = userId;
      userLanguage.LanguageId = await ...
    }
    

    For more information, see the "avoid async void" guidance in my Async Best Practices article.

    0 讨论(0)
提交回复
热议问题