Пример #1
0
        /// <summary>
        /// Removes A Subtopic from being attached to a Category in a Junction Table in the Database
        /// </summary>
        /// <param name="SubtopicName">Name of the Subtopic to be dettached</param>
        /// <param name="CategoryName">Name of the Category</param>
        public void RemoveSubtopicFromCategory(string SubtopicName, string CategoryName)
        {
            //DISCLAIMER: ; in hindsight it might have been easier to pass in the id of the subtopic name and the id of the category name. The belief was that this method would be easier to use with knowledge of the subtopic and category name without going back and forth to the database to find those things out.


            int subtopicID = 0;
            int categoryID = 0;

            EAD.Subtopic removedTopic = new EAD.Subtopic();
            try
            {
                foreach (var item in db.Subtopic) //Gets the subtopicID which will be needed so it can be removed
                {
                    if (item.Subtopic_Name == SubtopicName)
                    {
                        subtopicID = item.Subtopic_ID;
                    }
                }
            }
            catch (Exception ex)
            {
                // to do
            }

            try
            {
                foreach (var item in db.Categories) //Gets the categoryID which will be needed so it can be removed
                {
                    if (item.Categories_Name == CategoryName)
                    {
                        categoryID = item.Categories_ID;
                    }
                }
            }
            catch (Exception ex)
            {
                // to do
            }
            try
            {
                foreach (var item in db.Categories_Subtopic) //Finds the row on the junction table that contains the pair of values and removes it
                {
                    if (item.Subtopic_ID == subtopicID && item.Categories_ID == categoryID)
                    {
                        db.Categories_Subtopic.Remove(item); //Remove that row where both ID's fit that condition
                    }
                }
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                //to do
            }
        }
Пример #2
0
        /// <summary>
        /// Deletes a Subtopic in the Subtopic Database based on the name
        /// </summary>
        /// <param name="SubtopicName">Subtopic Name for removal</param>
        public void DeleteSubtopic(string SubtopicName)
        {
            int subtopicID = 0;

            EAD.Subtopic removedTopic = new EAD.Subtopic(); //Make a Data Access Library Subtopic Object to prepare to remove this object from the database
            try
            {
                foreach (var item in db.Subtopic) //Gets the subtopicID which will be needed so it can be removed
                {
                    if (item.Subtopic_Name == SubtopicName)
                    {
                        subtopicID   = item.Subtopic_ID;
                        removedTopic = item;
                    }
                }
            }
            catch (Exception ex)
            {
                // to do
            }

            try
            {
                foreach (var item in db.Categories_Subtopic) //Removes all references to the subtopic in the database
                {
                    if (item.Subtopic_ID == subtopicID)
                    {
                        db.Categories_Subtopic.Remove(item);
                    }
                }

                db.Subtopic.Remove(removedTopic); // removes the subtopic from the subtopic table.
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                // to do
            }
        }