public override void Run() { using (LabDbEntities context = new LabDbEntities()) { Material lastMaterial; List <List <Material> > duplicates = new List <List <Material> >(); List <Material> currentGroup; IList <Material> matList = context.Materials.AsNoTracking() .OrderBy(mat => mat.AspectID) .ThenBy(mat => mat.LineID) .ThenBy(mat => mat.TypeID) .ThenBy(mat => mat.RecipeID) .ToList(); { lastMaterial = matList[0]; currentGroup = new List <Material>(); foreach (Material mat in matList.Skip(1)) { if (mat.AspectID == lastMaterial.AspectID && mat.LineID == lastMaterial.LineID && mat.TypeID == lastMaterial.TypeID && mat.RecipeID == lastMaterial.RecipeID) { currentGroup.Add(mat); } else { if (currentGroup.Count > 1) { duplicates.Add(currentGroup); } currentGroup = new List <Material>() { mat }; } lastMaterial = mat; } foreach (List <Material> duplicateGroup in duplicates) { Material parent = duplicateGroup[0]; foreach (Material toSubstitute in duplicateGroup.Skip(1)) { foreach (Batch btc in toSubstitute.Batches) { btc.MaterialID = parent.ID; } context.Entry(toSubstitute).State = System.Data.Entity.EntityState.Deleted; } } context.SaveChanges(); } } }
public static void Create(this ExternalConstruction entry) { using (LabDbEntities entities = new LabDbEntities()) { entities.ExternalConstructions.Attach(entry); entities.Entry(entry).State = System.Data.Entity.EntityState.Added; entities.SaveChanges(); } }
/// <summary> /// Runs the Command Routine over a given context /// </summary> /// <param name="context">A LabDbEntities context</param> public override void Execute(LabDbEntities context) { _context = context; Batch attachedBatch = _context.Batches.Find(_batchInstance.ID); _context.Entry(attachedBatch).CurrentValues.SetValues(_batchInstance); attachedBatch.Material = GetOrCreateMaterial(_materialTemplate); _context.SaveChanges(); }
public static void Delete(this ExternalConstruction entry) { using (LabDbEntities entities = new LabDbEntities()) { entities.Entry(entities .ExternalConstructions .First(exc => exc.ID == entry.ID)) .State = System.Data.Entity.EntityState.Deleted; entities.SaveChanges(); entry.ID = 0; } }
public static void Delete(this InstrumentUtilizationArea entry) { // Deletes an InstrumentUtilizationArea entry using (LabDbEntities entities = new LabDbEntities()) { entities.Entry(entities .InstrumentUtilizationAreas .First(iua => iua.ID == entry.ID)) .State = EntityState.Deleted; entities.SaveChanges(); entry.ID = 0; } }
public static void Delete(this User entry) { // Deletes a User entry using (LabDbEntities entities = new LabDbEntities()) { entities.Entry(entities.Users .First(usr => usr.ID == entry.ID)) .State = EntityState.Deleted; entities.SaveChanges(); entry.ID = 0; } }
public static void Delete(this CalibrationReport entry) { // Deletes a Calibration entry from the DB using (LabDbEntities entities = new LabDbEntities()) { entities.Entry(entities .CalibrationReports .First(crep => crep.ID == entry.ID)) .State = System.Data.Entity.EntityState.Deleted; entities.SaveChanges(); entry.ID = 0; } }
public override void Execute(LabDbEntities context) { _context = context; foreach (Batch batchTemplate in _batchList) { using (var transaction = _context.Database.BeginTransaction()) { Batch newBatch = new Batch(); context.Batches.Add(newBatch); context.Entry(newBatch).CurrentValues.SetValues(batchTemplate); newBatch.Material = context.Materials.First(mat => mat.Aspect.Code == batchTemplate.Material.Aspect.Code && mat.MaterialLine.Code == batchTemplate.Material.MaterialLine.Code && mat.Recipe.Code == batchTemplate.Material.Recipe.Code && mat.MaterialType.Code == batchTemplate.Material.MaterialType.Code); if (batchTemplate.Material.ExternalConstruction != null) { newBatch.Material.ExternalConstructionID = batchTemplate.Material.ExternalConstruction.ID; } if (batchTemplate.Material.Project != null) { newBatch.Material.ProjectID = batchTemplate.Material.Project.ID; } if (batchTemplate.Material.Recipe.Colour != null) { newBatch.Material.Recipe.ColourID = batchTemplate.Material.Recipe.Colour.ID; } try { transaction.Commit(); } catch { _failedBatches.AddLast(batchTemplate); } } } context.SaveChanges(); }
/// <summary> /// Updates all the Requirement entities for an Updated MethodVariant. /// Creates/Updates/Deletes Corresponding SubRequirements in order to Match the current SubMethod list of the Method /// </summary> /// <param name="oldVariant">The MethodVariant to be replaced</param> /// <param name="newVariant">The MethodVariant that will replace the old one</param> /// <param name="newMethod">A reference to the new Method instance</param> /// <param name="entities">A reference to the DB transaction instance</param> private void BuildUpdatedRequirementsForMethodVariant(MethodVariant oldVariant, MethodVariant newVariant, Method newMethod, LabDbEntities entities) { foreach (Requirement req in oldVariant.Requirements.ToList()) { req.MethodVariant = newVariant; foreach (SubMethod smtd in newMethod.SubMethods) { SubRequirement updateTarget = req.SubRequirements.FirstOrDefault(sreq => sreq.SubMethodID == smtd.OldVersionID); if (updateTarget != null) { updateTarget.SubMethod = smtd; updateTarget.IsUpdated = true; } else { req.SubRequirements.Add(new SubRequirement() { IsUpdated = true, RequiredValue = "", SubMethod = smtd, }); } } foreach (SubRequirement sreq in req.SubRequirements.Where(nn => !nn.IsUpdated) .ToList()) { entities.Entry(sreq).State = EntityState.Deleted; } } }