//TODO: Refactor
 public void addCategoriesToDocument(List<string> categoryList, int documentId)
 {
     List<Category> allCategories = (
                                     from c in this.Categories
                                     select c
                                     ).ToList();
     List<string> allCategoryNames = new List<string>();
     foreach(Category cat in allCategories)
     {
         allCategoryNames.Add(cat.Name);
     }
     foreach (string categoryToBeAdded in categoryList)
     {
         if (allCategoryNames.Contains(categoryToBeAdded))
         {
             //No need to add the category. Just add the association
             Category category = allCategories.Find(delegate(Category c) { return c.Name == categoryToBeAdded; });
             CategoryDocument catDoc = new CategoryDocument { CategoryId = category.Id, DocumentId = documentId };
             CategoryDocuments.InsertOnSubmit(catDoc);
             this.SubmitChanges();
         }
         else
         {
             //The category does not already exist. Add a new category object too.
             Category category = new Category { Name = categoryToBeAdded };
             Categories.InsertOnSubmit(category);
             this.SubmitChanges();
             Category insertedCategory = (from c in this.Categories
                                          where c.Name == categoryToBeAdded
                                          select c).First();
             CategoryDocument catDoc = new CategoryDocument { DocumentId = documentId, CategoryId = insertedCategory.Id };
             CategoryDocuments.InsertOnSubmit(catDoc);
             this.SubmitChanges();
         }
     }
 }
 partial void DeleteCategoryDocument(CategoryDocument instance);
 partial void UpdateCategoryDocument(CategoryDocument instance);
 partial void InsertCategoryDocument(CategoryDocument instance);
		private void detach_CategoryDocuments(CategoryDocument entity)
		{
			this.SendPropertyChanging();
			entity.Category = null;
		}
		private void attach_CategoryDocuments(CategoryDocument entity)
		{
			this.SendPropertyChanging();
			entity.Document = this;
		}