In Parallel.ForEach: the underlying provider refused Open. with EF5

This code will work sometimes for some elements, but it always fails when trying to process more elements, and I would get the following: {"The underlying provider failed on Open."}exception

  List<Recon> scenarioAll = db.Transactions
                  .Where(t => t.SrcObjTyp == "13")
                  .Select(t => t.Recon).ToList();


  //db.Transactions.Where(t => t.SrcObjTyp == "13").ToList().ForEach(t => reconsWithType13Trans.Add(t.Recon));

  Parallel.ForEach(scenarioAll.Take(100), r =>
  {

    // ### Exception : {"The underlying provider failed on Open."} here ###
    invoices = r.Transactions.SelectMany(t => t.InvoiceDetails).ToList();


    CreateFacts(invoices, r/*, db*/).ForEach(f => facts.Add(f));
    transactions = r.Transactions.Where(t => !t.SrcObjTyp.Contains("13")).ToList();
    DistributeTransactionOnItemCode(transactions, facts);
    Console.WriteLine(i += 1);
  });

  facts.ForEach(f => db.ReconFacts.Add(f));
  db.SaveChanges();

I think issus is a multiple process that accesses the database at the same time, is it possible to allow EF to be requested by several processes this way?

There is also a way to load everything into memory, so when accessing a child Reconlike this, r.Transactions.SelectMany(t => t.InvoiceDetails).ToList();everything will be in memory and will not access the base database?

Which solution do you think is best?

+4
source share
1

, , "". ...

List<Recon> scenarioAll = db.Transactions
              .Where(t => t.SrcObjTyp == "13")
              .Select(t => t.Recon)
              .Include(r => r.Transactions.Select(t => t.InvoiceDetails))
              .ToList();

http://msdn.microsoft.com/en-us/library/gg671236(v=vs.103).aspx

, Lazy Loading, .

, , ... ...

 var query =  = db.Transactions
              .Where(t => t.SrcObjTyp == "13")
              .Select(t => t.Recon);
 var scenarioAll = query.ToList();
 var invoicesByReconIdQuery = from r in query
                              from t in r.Transactions
                              from i in t.InvoiceDetails
                              group i by r.Id into g
                              select new { Id = g.Key, Invoices = g.ToList() };


 var invoicesByReconId = invoicesByReconIdQuery.ToDictionary(x => x.Id, x => x.Invoices);

 Parallel.ForEach(scenarioAll.Take(100), r =>
 {

     // ### Exception : {"The underlying provider failed on Open."} here ###
     var invoices = invoicesByReconId[r.Id];
 }
+2

All Articles