title: Asynchronously saving to multiple tables in a single SQL Server Db
body: <p>I was able to populate multiple tables of a database in parallel using Task.WhenAll(tasks). I have a generic method for each table:</p>
  
  <pre><code>var tasks = new List&lt;Task&gt;()
          {
              CheckNewItems&lt;S, T&gt;(async () =&gt; await GetPrimaryKeys&lt;S, T&gt;(),
              CheckNewItems&lt;S, T&gt;(async () =&gt; await GetPrimaryKeys&lt;S, T&gt;(),
              CheckNewItems&lt;S, T&gt;(async () =&gt; await GetPrimaryKeys&lt;S,T&gt;(),                                                                     
              CheckNewItems&lt;S, T&gt;(async () =&gt; await GetPrimaryKeys&lt;S,T&gt;()
          };
  
          await Task.WhenAll(tasks);
  </code></pre>
  
  <p>Now a new requirement came that asks me to save the number of new items from that async method, and I stumbled into <a href="https://stackoverflow.com/questions/17197699/awaiting-multiple-tasks-with-different-results">this</a> post. I tried to do it the way its mentioned in the comments on the accepted answer, to get the number of new items:</p>
  
  <pre><code>   int newItems = 0;
     newItems += await CheckNewItems&lt;S, T&gt;(async () =&gt; await GetPrimaryKeys&lt;S, T&gt;();
     newItems += await CheckNewItems&lt;S, T&gt;(async () =&gt; await GetPrimaryKeys&lt;S, T&gt;();
     newItems += await CheckNewItems&lt;S, T&gt;(async () =&gt; await GetPrimaryKeys&lt;S,T&gt;();                                                                     
     newItems += await CheckNewItems&lt;S, T&gt;(async () =&gt; await GetPrimaryKeys&lt;S,T&gt;();
  </code></pre>
  
  <p>Using the second method, the database tables do not update in parallel but synchronously.</p>
  
  <p>Inside that CheckNewItem method, I'm using the SaveChangesAsync and AddRangeAsync methods in EF Core. I'm not sure why using Task.WhenAll(tasks) does the desired action and doing it the second way doesn't when in the <a href="https://stackoverflow.com/questions/17197699/awaiting-multiple-tasks-with-different-results">post</a> some of the comments mentioned that you don't need to do Task.WhenAll to make sure async methods run in parallel. </p>
  
  <p>I would like to get the results of each call to CheckNewItems, while they can still save to the database asynchronously and in parallel as usual. Thanks in advance for the insights and help :)</p>