public ActionResult Edit(PatientViewModel patientVM) { var patient = db.Patients.Find(patientVM.Patient.ID); patient.FirstName = patientVM.Patient.FirstName; patient.LastName = patientVM.Patient.LastName; foreach(var ca in patientVM.CompleteAttributes) { var patientAttribute = db.Patient_PatientAttributes.Include(j=>j.PatientAttribute).SingleOrDefault(i => i.PatientID == patient.ID && i.PatientAttributeID == ca.SelectedPatientAttributeValue.PatientAttributeID); //this is null if a new attribute has been added recently, and the patient hasn't been assigned an attribute value for this attribute if (patientAttribute == null) { patientAttribute = new Patient_PatientAttribute { PatientAttributeID = ca.SelectedPatientAttributeValue.PatientAttributeID, PatientAttributeValueID = ca.SelectedPatientAttributeValue.ID, PatientID = patient.ID }; db.Patient_PatientAttributes.Add(patientAttribute); db.SaveChanges(); } else { if(patientAttribute.PatientAttribute.Numeric) { patientAttribute.PatientAttributeValue.Value = ca.SelectedPatientAttributeValue.Value; db.Entry(patientAttribute).State = EntityState.Modified; } else { patientAttribute.PatientAttributeValueID = ca.SelectedPatientAttributeValue.ID; db.Entry(patientAttribute).State = EntityState.Modified; } } } if (ModelState.IsValid) { db.Entry(patient).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(patient); }
public ActionResult Create(PatientViewModel patientVM) { var patient = new Patient { FirstName = patientVM.Patient.FirstName, LastName = patientVM.Patient.LastName }; if (ModelState.IsValid) { db.Patients.Add(patient); db.SaveChanges(); } using (var db = this.db) { using (var transaction = db.Database.BeginTransaction()) //this allows you to have two savechanges() in one function { try { var patient_PatientAttributesList = new List<Patient_PatientAttribute>(); foreach (var ca in patientVM.CompleteAttributes) { Patient_PatientAttribute patientAttribute = null; if (ca.PatientAttribute.Numeric) { //for numeric attributes, the attribute value assigned to the patient is contained in the AttributeValue table, and then also referenced //in the Patient_PatientAttributes table var attributeValue = new PatientAttributeValue { PatientAttributeID = ca.PatientAttribute.ID, Value = ca.SelectedPatientAttributeValue.Value }; db.PatientAttributeValues.Add(attributeValue); db.SaveChanges(); patientAttribute = new Patient_PatientAttribute { PatientAttributeID = ca.PatientAttribute.ID, PatientAttributeValueID = attributeValue.ID, PatientID = patient.ID }; } else { patientAttribute = new Patient_PatientAttribute { PatientAttributeID = ca.PatientAttribute.ID, PatientAttributeValueID = ca.SelectedPatientAttributeValue.ID, PatientID = patient.ID }; } patient_PatientAttributesList.Add(patientAttribute); } if (ModelState.IsValid) { foreach (var pa in patient_PatientAttributesList) { db.Patient_PatientAttributes.Add(pa); } //db.Patient_PatientAttributes.AddRange(patientsAttributesList); db.SaveChanges(); transaction.Commit(); } } catch (Exception) { transaction.Rollback(); } } } return RedirectToAction("Index"); }