Most correct custom Db update

,

Hi,

I need some support on Data Directive script. I want to hear your opinion on what is the best/correct way to make db update in custom code. At first I did it without txScope, but added Db .Validation(). Bpm does what it has to do, but exception pops up. Now I added txScope, but still not sure if this is correct. adding custom code which supose to caunt how many jobs has beent closed for particular part and revision.

int Batch;

foreach(var add_one in ttJobHead.Where(P=>P.Company == "SFU" && ! P.JobNum.StartsWith("PVZ"))) // Check if it's not a sample
{
  Batch = 1; // Default value for firs job closing since there are no history and job head has no closing dates
  foreach(var count in Db.JobHead.With(LockHint.NoLock).Where(J=>J.Company == add_one.Company && J.PartNum == add_one.PartNum && J.RevisionNum == add_one.RevisionNum && J.ClosedDate <= add_one.ClosedDate && J.JobNum != add_one.JobNum && ! J.JobNum.StartsWith("PVZ") && J.JobClosed == true))
    {
      Batch = Batch + 1; // append 1 for each counted job
    }
    using (var txScope = IceContext.CreateDefaultTransactionScope())
    {
  foreach(var add_rev_batch in Db.PartRev.With(LockHint.UpdLock).Where(R=>R.Company == add_one.Company && R.PartNum == add_one.PartNum && R.RevisionNum == add_one.RevisionNum)) // find coresponding PartNum and Revision for ttJobHead
  {
    add_rev_batch.BatchCounter_c = Batch + add_rev_batch.E9_Bacth_Counter_c ; // Make a SUM from calculated and historical E9 values and append it to PartRev.BatchCounter column
  }
   Db.Validate();
   txScope.Complete();
     }
}