title: Calling Stored procedure inside a thread to update multiple records
body: <p>I am trying to calla stored procedure for various unique entities . The stored procedure for a single entity takes about 33 secs. So I decided to call it using threads.</p>
  
  <p>Here are some of trials I have done :</p>
  
  <pre><code>public bool ExecuteTaxRateLinkingParallel(int mapID, int createdBy)
  {
      try
      {
          int snapshotID = (int)(HttpContext.Current.Session[GlobalConstant.snapShotID]);
          List&lt;TaxEntity&gt; taxEntities = new List&lt;TaxEntity&gt;();
          List&lt;Task&gt; tasks = new List&lt;Task&gt;();
          using (var ctx = new TopazDbContainer())
          {
              taxEntities = ctx.TaxEntities.AsParallel().Where(t =&gt; t.IsActive == true).ToList&lt;TaxEntity&gt;();
          }
  
          Parallel.ForEach&lt;TaxEntity&gt;(taxEntities, (entity) =&gt;
          {
              //SqlConnection connection; SqlTransaction trans; SqlCommand command;
              // break this into pieces of 5
              var task = Task.Factory.StartNew(() =&gt;
              {
                  using (var pctx = new TopazDbContainer())
                  {
                      try
                      {
                          int taxEntityID = entity.TaxEntityID;
                          pctx.CommandTimeout = 5000;
                          //string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["TOPAZDBConnectionStringParallel"].ConnectionString;
                          //connection = new SqlConnection(connectionString);
                          //command = new SqlCommand("dbo.[Usp_TaxRatesLinkingParallel]", connection);
                          //trans = connection.BeginTransaction();
                          //command.CommandType = CommandType.StoredProcedure;
                          //command.Parameters.AddWithValue("@MapID", mapID);
                          //command.Parameters.AddWithValue("@UserID", createdBy);
                          //command.Parameters.AddWithValue("@TaxEntityID", taxEntityID);
                          //command.Parameters.AddWithValue("@SnapshotID", snapshotID);
                          //connection.Open();
                          //command.CommandTimeout = 5000;
                          //command.ExecuteReader().AsParallel();
  
                          pctx.ContextOptions.LazyLoadingEnabled = true;
  
                          //pctx.ExecuteStoreCommand("Exec [Usp_TaxRatesLinkingParallel] @MapID={0},@UserID={1},@TaxEntityID={2},@SnapshotID{3}", new SqlParameter("MapID", mapID), new SqlParameter("UserID", createdBy), new SqlParameter("TaxEntityID", taxEntityID), new SqlParameter("SnapshotID", snapshotID));
                          var param = new DbParameter[] { new SqlParameter("UserID", createdBy), new SqlParameter("TaxEntityID", taxEntityID), new SqlParameter("SnapshotID", snapshotID) };
                          pctx.ExecuteStoreCommand("Exec [Usp_TaxRatesLinkingParallel] @MapID,@UserID,@TaxEntityID,@SnapshotID", param);
  
                          //var result = output.FirstOrDefault();
                      }
                      catch (TaskCanceledException tx)
                      {
                      }
                      catch (Exception e)
                      {
                      }
                      finally
                      {
                          pctx.SaveChanges();
                          pctx.Connection.Close();
                      }
                  }
              }, TaskCreationOptions.PreferFairness);
  
              tasks.Add(task);
  
              try
              {
                  Task.WaitAll(tasks.ToArray());
              }
              catch (AggregateException ae)
              {
                  ae.Handle((x) =&gt;
                  {
                      if (x is UnauthorizedAccessException)
                      {
                          return true;
                      }
                      else
                      {
                          return false;
                      }
                  });
              }
              catch (Exception ex)
              {
                  throw ex;
              }
          });
  
          return true;
      }
      catch (Exception ex)
      {
          TopazErrorLogs.AddTopazErrorLogBL(ex, 1, 1);
          throw new TopazCustomException(GlobalConstant.errorMessage);
      }
  }
  </code></pre>
  
  <p>For some the above statements the SP seems like it runs fine but when I check from the application or from backend the records doesn't get updated.</p>
  
  <p>Need help!</p>