public static async Task DeleteProjectAsync(int projectID) { await Task.Run(() => { using (var db = new ChContext()) { var project = db.Projects.Where(p => p.ID == projectID).First(); List <ProjectPlan> plans = new List <ProjectPlan>(); List <Reaction> reactions = new List <Reaction>(); StartingMaterial sm = new StartingMaterial(); List <Reagent> reagents = new List <Reagent>(); List <Solvent> solvents = new List <Solvent>(); List <Product> products = new List <Product>(); List <ObservationImg> observationImgs = new List <ObservationImg>(); plans = db.ProjectPlans.Where(pl => pl.ProjectID == projectID).ToList(); db.ProjectPlans.RemoveRange(plans); reactions = db.Reactions.Where(r => r.ProjectID == projectID).ToList(); foreach (var item in reactions) { if (item.NextStep.Count != 0) { throw new Exception("One or more reaction cannot be deleted because used as previous step for another reaction!"); } } foreach (var item in reactions) { sm = db.StartingMaterials.Where(stm => stm.ReactionID == item.ID).First(); db.StartingMaterials.Remove(sm); reagents = db.Reagents.Where(r => r.ReactionID == item.ID).ToList(); db.Reagents.RemoveRange(reagents); solvents = db.Solvents.Where(s => s.ReactionID == item.ID).ToList(); db.Solvents.RemoveRange(solvents); products = db.Products.Where(pr => pr.ReactionID == item.ID).ToList(); db.Products.RemoveRange(products); observationImgs = db.ObservationImgs.Where(obs => obs.ReactionID == item.ID).ToList(); db.ObservationImgs.RemoveRange(observationImgs); db.SaveChanges(); } db.Reactions.RemoveRange(reactions); db.Projects.Remove(project); db.SaveChanges(); } }); }
public static async Task DeleteReactionAsync(int reactionID) { await Task.Run(() => { using (var db = new ChContext()) { var reaction = db.Reactions.Where(r => r.ID == reactionID).First(); if (reaction.NextStep.Count != 0) { throw new Exception("One or more reaction cannot be deleted because used as previous step for another reaction!"); } StartingMaterial sm = new StartingMaterial(); List <Reagent> reagents = new List <Reagent>(); List <Solvent> solvents = new List <Solvent>(); List <Product> products = new List <Product>(); List <ObservationImg> observationImgs = new List <ObservationImg>(); sm = db.StartingMaterials.Where(stm => stm.ReactionID == reactionID).First(); db.StartingMaterials.Remove(sm); reagents = db.Reagents.Where(r => r.ReactionID == reactionID).ToList(); db.Reagents.RemoveRange(reagents); solvents = db.Solvents.Where(s => s.ReactionID == reactionID).ToList(); db.Solvents.RemoveRange(solvents); products = db.Products.Where(pr => pr.ReactionID == reactionID).ToList(); db.Products.RemoveRange(products); observationImgs = db.ObservationImgs.Where(obs => obs.ReactionID == reactionID).ToList(); db.ObservationImgs.RemoveRange(observationImgs); db.Reactions.Remove(reaction); db.SaveChanges(); } }); }