private Boolean checkBranch(Patient patient, Node parentNode, Node childNode)
        {
            if (parentNode.PatientAttributeNode)
            {
                var patientAttribute = db.PatientAttributes.Single(i => i.ID == parentNode.NodeValue);
                var patientsPatientAttributeValue = db.Patient_PatientAttributes.Single(i => i.PatientID == patient.ID && i.PatientAttributeID == patientAttribute.ID).PatientAttributeValue;
                int number1;
                int number2;
                switch (childNode.EdgeOperator)
                {
                    case "==":
                        var edgePatientAttributeValue = db.PatientAttributeValues.Single(i => i.ID == childNode.EdgeValue);
                        if (edgePatientAttributeValue.ID == patientsPatientAttributeValue.ID)
                            return true;
                        else
                            return false;
                    case "<=":
                        number1 = int.Parse(patientsPatientAttributeValue.Value);
                        number2 = childNode.EdgeValue;

                        if (number1 <= number2)
                            return true;
                        else
                            return false;
                    case ">":
                        number1 = int.Parse(patientsPatientAttributeValue.Value);
                        number2 = childNode.EdgeValue;

                        if (number1 > number2)
                            return true;
                        else
                            return false;
                }
            }
            else //node is an equipment node
            {
                var equipment = db.Equipment.Find(parentNode.NodeValue);
                var equipmentAttribute = db.EquipmentAttributes.Find(parentNode.SecondaryNodeValue);
                var edgeValue = childNode.EdgeValue;

                if (edgeValue == equipmentAttribute.CurrentEquipmentAttributeValueID)
                    return true;
                else
                    return false;
            }
            return false; //this should never be executed
        }
        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");
        }