public static void InsertOnSubmit(this DbSet<Category> source, Category category) { if (category.CategoryID == default(int)) { // New entity source.Add(category); } else { // Existing entity source.Attach(category); } }
public ActionResult CreateCategory(string title, int? importance, string imageUrl, string description) { if (!String.IsNullOrEmpty(title) && !String.IsNullOrEmpty(imageUrl) && !String.IsNullOrEmpty(description)) { try { TheBeerHouseDataContext dc = new TheBeerHouseDataContext(); Category category = new Category { Title = title, Importance = importance ?? -1, ImageUrl = imageUrl, Description = description, AddedBy = User.Identity.Name, AddedDate = DateTime.Now, Path = title.ToUrlFormat() }; dc.Categories.InsertOnSubmit(category); // save changes to database dc.SubmitChanges(); TempData["SuccessMessage"] = "Your category has been created."; return RedirectToAction("ManageArticles"); } catch (Exception exc) { TempData["ErrorMessage"] = exc.Message; } } ViewData["categorytitle"] = title; ViewData["importance"] = importance; ViewData["imageUrl"] = imageUrl; ViewData["description"] = description; ViewBag.Title = "Create Category"; return View("CreateCategory"); }
public static void DeleteOnSubmit(this DbSet<Category> source, Category category) { source.Remove(category); }