Пример #1
0
 /// <summary>
 /// Adds New subject to the database using SubjectName
 /// </summary>
 /// <param name="SubjectName">New Subject Name</param>
 public void AddSubject(string SubjectName)
 {
     EAD.Subject addedSubject = new EAD.Subject();
     addedSubject.Subject_Name = SubjectName;
     try
     {
         db.Subject.Add(addedSubject); //adds object to database
         db.SaveChanges();
     }
     catch (Exception ex)
     {
         //to do
     }
 }
Пример #2
0
        /// <summary>
        /// This Method Deletes a subject
        /// </summary>
        /// <param name="SubjectName">The name of the Subject up for deletion</param>
        public void DeleteSubject(string SubjectName)
        {
            int subjectID = 0;

            EAD.Subject removedSubject = new EAD.Subject();

            try
            {
                //Gets the subjectID which will be needed so it can be removed
                foreach (var item in db.Subject)
                {
                    if (item.Subject_Name == SubjectName)
                    {
                        subjectID      = item.Subject_ID;
                        removedSubject = item; //keeps a reference to the subject that will be removed
                    }
                }
            }
            catch (Exception ex)
            {
                // to do
            }

            try
            {
                //Removes all references to the subject in the database
                foreach (var item in db.Subject_Categories) //For each row in the Subject Categories Junction Table
                {
                    if (item.Subject_ID == subjectID)
                    {
                        db.Subject_Categories.Remove(item); //Remove the row from the junction table
                    }
                }
            }
            catch (Exception ex)
            {
                //to do
            }

            try
            {
                db.Subject.Remove(removedSubject); // removes the subject from the subtopic table.
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                //to do
            }
        }