title: How to use TPL with Entity Framework?
body: <p>I am looking forward to use TPL with Entity Framework 6.1. I have already coded a Demo Application with this;</p>
  
  <p>Explanation What i am trying to Achieve:</p>
  
  <p>CODE :</p>
  
  <pre><code>     public void StoreTeamDetailsFromServer()
          {
              try
              {
                  IAccountRepository accountRepository = new AccountRepository();
                  var userDetails = accountRepository.GetLoggedInUserDetails();
  
                  int pageSize = 10;
  
                  var departmentApiParams = new
                  {
                      username= userDetails.UserName,
                      password= userDetails.Password,
                      sort_by = "a-z",
                      filter_by = "all",
                      n_records = pageSize,
                      index_from = 1 
  
                  };
  
                  /* Generic Code Repository to get my details for JSON RPC */
  
                  JsonRpcRequest getDepartmentDetailsRequestParams = new JsonRpcRequest() { ApiMethod = "department_list", Params = departmentApiParams, Url = "http://ServerAddress/index.php" };
  
                  JsonWebRequestRpcRepository&lt;DepartmentWrapper&gt; jsonRpcClientRepository = new JsonWebRequestRpcRepository&lt;DepartmentWrapper&gt;();
  
  
                  /* Actual call to my Generic JSON RPC Repository */
  
                  var departmentResponse = jsonRpcClientRepository.RpcCall(getdepartmentDetailsRequestParams).Response;
                  /* This is the Task which is created to Start saving the data of the First Page*/
  
                  Task departmentTasks =  Task.Run(() =&gt; SaveDepartments(Mapper.Map&lt;List&lt;DepartmentResponse&gt;, List&lt;Departments&gt;&gt;(departmentResponse.departmentDetails)));
                  /* This is where it queries it the Result has more Records i.e. more Pages */
  
                  if (departmentResponse.HasMoreRecords)
                  {
                      /* This is where the Paging logic is taking place  */
  
                      int totalRecords = departmentResponse.TotalCount;
                      int totalPages = Convert.ToInt32(Math.Ceiling((decimal)totalRecords / pageSize));
                      for (int page = 0; page &lt; totalPages; page++)
                      {
  
                          /* This is calculated for the next page request formation */
                          var currentIndex = departmentResponse.DepartmentDetails.Max(x =&gt; x.Index);
  
                        /* This is where the Task is getting created Repeated for saving the further pages in the data */
                          departmentResponse = jsonRpcClientRepository.RpcCall(GetJsonRpcDepartmentParams(userDetails.UserName,userDetails.Password,currentIndex,pageSize)).Response;
                          departmentTasks = Task.Run(() =&gt;  SaveDepartments(Mapper.Map&lt;List&lt;DepartmentResponse&gt;, List&lt;Departments&gt;&gt;(departmentResponse.DepartmentDetails)));
  
                      }
  
                  }
                  Console.WriteLine(DepartmentResponse.TotalCount + " Departments added into the Database");
  
              }
              catch
              {
                  throw;
              }
  
          }
  
         private void SaveDepartments(List&lt;Departments&gt; departmentList)
         {
          try
          {
              IDepartmentRepository DepartmentRepository = new DepartmentRepository();
  
              DepartmentRepository.AddOrUpdateDepartments(departmentList);
          }
          catch
          {
              throw;
          }
        }
  </code></pre>
  
  <p>I have a Web JSON-RPC call which fetches data for me. Now this data is paged, and I am wanting to use this as Fetch the data - Start a TASK (TPL) for saving the data - simultaneously call for the Next page of the data (which when returned again will start a task for saving the data).</p>
  
  <p>Now, What happens is that, the Task creation etc stuff works well and the Data starts getting saved too. BUT Problem arises for the Last of the Pages, the data passed in the DepartmentRepository. The Console.WriteLine("") gets over first and then the Tasks go for saving the data.</p>
  
  <p>So ideally what is happening according to me is that "Context" i.e. the data which i have passed to the Tasks gets lost since the Execution goes out of the Scope.</p>
  
  <p>What is the Best way to Handle this ? </p>