public static void ConnectLocationMoleculestatic(string cas, string location, double?m, double?V) { using (var db = new ChContext()) { var thisMolecule = db.MoleculeStatics.Where(molec => molec.CAS == cas).FirstOrDefault(); var thisLocation = db.Locations.Where(loc => loc.Code == location).FirstOrDefault(); var thisLocationMolecule = new LocationMolecule(); thisLocationMolecule.Location = thisLocation; thisLocationMolecule.MoleculeStatic = thisMolecule; if (m.HasValue) { thisLocationMolecule.m = m.Value; } else { if (V.HasValue) { thisLocationMolecule.v = V.Value; } else { //throw new Exception("No m and V values in excel"); } } db.LocationMolecules.Add(thisLocationMolecule); db.SaveChanges(); } }
public static async Task AddReactionAsync(ReactionDTO reactionDTO) { await Task.Run(() => { using (var db = new ChContext()) { var reaction = reactionDTO.TransformToReaction(); reaction.Chemist = db.People.Where(p => p.Name == reactionDTO.Chemist).First(); reaction.Chiefchemist = db.People.Where(p => p.Name == reactionDTO.Chiefchemist).First(); reaction.Project = db.Projects.Where(p => p.Name == reactionDTO.Project).First(); if (!string.IsNullOrEmpty(reactionDTO.PreviousStep)) { reaction.PreviousStep = db.Reactions.Where(re => re.ReactionCode == reactionDTO.PreviousStep).First(); } if (!reaction.Sketch.Value) { foreach (var item in reactionDTO.ObservationImgs) { var tmp = new ObservationImg { img = item, Reaction = reaction }; db.ObservationImgs.Add(tmp); reaction.ObservationImgs.Add(tmp); } } var tmpsm = reactionDTO.StartingMaterial.TransformToStartingMaterial(); tmpsm.Reaction = reaction; db.StartingMaterials.Add(tmpsm); reaction.StartingMaterials.Add(tmpsm); foreach (var item in reactionDTO.Reagents) { var tmp = item.TransformToReagent(); tmp.Reaction = reaction; db.Reagents.Add(tmp); reaction.Reagents.Add(tmp); } foreach (var item in reactionDTO.Solvents) { var tmp = item.TransformToSolvent(); tmp.Reaction = reaction; db.Solvents.Add(tmp); reaction.Solvents.Add(tmp); } foreach (var item in reactionDTO.Products) { var tmp = item.TransformToProduct(); tmp.Reaction = reaction; db.Products.Add(tmp); reaction.Products.Add(tmp); } db.Reactions.Add(reaction); db.SaveChanges(); //var sm1 = db.LocationMolecules.Where(m => m.MoleculeCAS == reactionDTO.StartingMaterial.MoleculeCAS && m.Location.Code == reactionDTO.StartingMaterial.Location).ToList(); var sm = db.LocationMolecules.Where(m => m.MoleculeCAS == reactionDTO.StartingMaterial.MoleculeCAS && m.Location.Code == reactionDTO.StartingMaterial.Location).First(); if (sm.v.HasValue) { sm.v = sm.v - reactionDTO.StartingMaterial.VValue; } else { sm.m = sm.m - reactionDTO.StartingMaterial.mValue; } db.SaveChanges(); LocationMolecule r = new LocationMolecule(); //LocationMolecule r2 = new LocationMolecule(); foreach (var item in reactionDTO.Reagents) { //var r2 = db.LocationMolecules.Where(m => m.MoleculeCAS == item.MoleculeCAS && m.Location.Code == item.Location).ToList(); r = db.LocationMolecules.Where(m => m.MoleculeCAS == item.MoleculeCAS && m.Location.Code == item.Location).First(); if (r.v.HasValue) { r.v = r.v - item.VValue; } else { r.m = r.m - item.mValue; } db.SaveChanges(); } LocationMolecule s = new LocationMolecule(); foreach (var item in reactionDTO.Solvents) { //var s2 = db.LocationMolecules.Where(m => m.MoleculeCAS == item.MoleculeCAS && m.Location.Code == item.Location).ToList(); s = db.LocationMolecules.Where(m => m.MoleculeCAS == item.MoleculeCAS && m.Location.Code == item.Location).First(); s.v = s.v - item.VValue; db.SaveChanges(); } } }); }