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

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)