title: Call multiple stored procedures using Async/Await and EntityFramework
body: <p>I have to call multiple stored procedures that consume time. Ideally those procedures have to exec in the same time but it raise lot of problems.</p>
  
  <p>Here is the simplified code :</p>
  
  <pre><code>private  async void refresh_Controle(object sender, RoutedEventArgs e)
  {
      SqlParameter param1 = new SqlParameter("@devicename", DeviceName);
      Task&lt;int&gt; mcResult = GenkaiBase.Database.ExecuteSqlCommandAsync("exec Refresh_McAfee @devicename", param1);
      int Mc = await mcResult;
  
      SqlParameter param2 = new SqlParameter("@devicename", DeviceName);
  
      Task&lt;int&gt; dcaiResult = GenkaiBase.Database.ExecuteSqlCommandAsync("exec Refresh_DCAI @devicename", param2);
      int Dc = await dcaiResult;
  }
  </code></pre>
  
  <p>This has 2 issues:</p>
  
  <ol>
  <li>those procedure execute one after the other</li>
  <li>if I call this more than once, I get a SQL Server error where one of the procedure is chosen as victim.</li>
  </ol>
  
  <p>I tried calling the two procedures in the same time with this code in an async method:</p>
  
  <pre><code>public async Task&lt;bool&gt; Refresh_Control(string devicename)
  {
      List&lt;Task&gt; Tlist = new List&lt;Task&gt;();
      Console.WriteLine("Launch Refresh");
  
      SqlParameter param1 = new SqlParameter("@devicename", devicename);
  
      Task&lt;int&gt; mcResult =  Genkai_db.Database.ExecuteSqlCommandAsync("exec Refresh_McAfee @devicename", param1);
  
      SqlParameter param2 = new SqlParameter("@devicename", devicename);
  
      Task&lt;int&gt; dcaiResult =  Genkai_db.Database.ExecuteSqlCommandAsync("exec Refresh_DCAI @devicename", param2);
  
      Console.WriteLine("all set");
  
      Tlist.Add(mcResult);
      Tlist.Add(dcaiResult);
  
      await Task.WhenAll(Tlist.ToArray());
      int mc = await mcResult;
      int dc = await dcaiResult;
  
      Console.WriteLine("Finish Refresh" + mc + dc);
      return true;
  }
  </code></pre>
  
  <p>The logic is fine for send thing simultaneous but the second procedure throw an error cause the first one isn't finish yet.</p>
  
  <p>Error translated by goole:</p>
  
  <blockquote>
    <blockquote>
      <p>An exception of type 'System.NotSupportedException' occurred in EntityFramework.dll but was not handled in user code</p>
    </blockquote>
    
    <p>Additional Information: A second operation was started in this context
    before a previous asynchronous operation has been completed. Use
    "await" to ensure that all asynchronous operations were completed
    before calling another method in this context. No member instance is
    guaranteed to be thread safe.</p>
  </blockquote>
  
  <p>So what is the deal, why can't I call several stored procedures at the same time without being stuck by SQL Server?</p>