title: A second operation started on this context before a previous asynchronous operation completed
body: <p><strong>Message:</strong></p>
  
  <pre><code>"System.NotSupportedException was unhandled
  Message: An unhandled exception of type 'System.NotSupportedException' occurred in mscorlib.dll
  Additional information: A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe."
  </code></pre>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>public async Task&lt;IEnumerable&lt;UserLangDTO&gt;&gt; ImportLang(int userId)
  {
      var userLangs = new List&lt;UserLangDTO&gt;();
      using (FirstContext ctx = new FirstContext())
      {
          if (await (ctx.UserLang.AnyAsync(u =&gt; u.UserId == userId)) == false)
              //some exception here
  
          userLangs = await ctx.UserLang.AsNoTracking()
                                  .Where(ul =&gt; ul.UserId == userId)
                                  .Join(ctx.Language,
                                      u =&gt; u.LangID,
                                      l =&gt; l.LangID,
                                      (u, l) =&gt; new { u, l })
                                  .Join(ctx.Level,
                                      ul =&gt; ul.u.LevelID,
                                      le =&gt; le.LevelID,
                                      (ul, le) =&gt; new { ul, le })
                                  .Select(r =&gt; new UserLangDTO
                                  {
                                  UserId = r.ul.u.UserId,
                                  Language = r.ul.l.Language,
                                  Level = r.le.Level,
                                  }).ToListAsync().ConfigureAwait(false);
  
      }
      using (SecondContext ctx = new SecondContext())
      {
          if ( await (ctx.UserLangs.AnyAsync(u =&gt; u.UserId == userId)) == true &amp;&amp; userLangs.Any())
              ctx.UserLangs.RemoveRange(ctx.UserLangs.Where(u =&gt; u.UserId == userId));
          if (await hasUserLangs &amp;&amp; userLangs.Any())
          {
              userLangs.ForEach(async l =&gt;
              {
                  var userLanguage = new UserLang();
                  userLanguage.UserId = userId;
                  userLanguage.LanguageId = await ctx.Languages.AsNoTracking()
                                                   .Where(la =&gt; la.NameEn == l.Language)
                                                   .Select(la =&gt; la.Id).FirstOrDefaultAsync().ConfigureAwait(false);
                  userLanguage.LevelId = await ctx.Levels.AsNoTracking()
                                                  .Where(la =&gt; la.NameEn == l.Language)
                                                  .Select(la =&gt; la.Id).FirstOrDefaultAsync().ConfigureAwait(false);
  
                  ctx.UserLangs.Add(userLanguage);
              });
          }
          await ctx.SaveChangesAsync().ConfigureAwait(false);
      }
      return userLangs;
  }
  </code></pre>
  
  <p><strong>What I Tried:</strong></p>
  
  <p>I'm not sure what I'm doing wrong, I tried different stuff like : </p>
  
  <p>1.</p>
  
  <pre><code>await Task.Run(() =&gt; Parallel.ForEach(strings, s =&gt;
  {
      DoSomething(s);
  })); 
  </code></pre>
  
  <p>2.</p>
  
  <pre><code>var tasks = userLangs.Select(async l =&gt;
  {
      //rest of the code here
  }
  await Task.WhenAll(tasks); 
  </code></pre>
  
  <p>3.</p>
  
  <pre><code>var tasks = userLangs.Select(async l =&gt;
  {
      //rest of the code here
  }
  await Task.WhenAll(tasks);
  
  await ctx.SaveChangesAsync().ConfigureAwait(false); 
  </code></pre>
  
  <ol start="4">
  <li>Other trial and error attempts, that i do not rember now</li>
  </ol>
  
  <p>What am I doing wrong?</p>