// 12.Update category public Boolean UpdateCategory(int categoryId, string name, string description) { using (var db = new NorthwindContex()) { if (name != null && name.Length <= 15 && (from category in db.Categories where category.Id == categoryId select category).Any()) { var category = (from c in db.Categories where c.Id == categoryId select c).First(); category.CategoryName = name; category.CategoryDescription = description; db.SaveChanges(); return(true); } return(false); } }
// 13.Delete category public Boolean DeleteCategory(int categoryId) { using (var db = new NorthwindContex()) { if ((from category in db.Categories where category.Id == categoryId select category).Any()) { var category = (from c in db.Categories where c.Id == categoryId select c).First(); db.Categories.Remove(category); db.SaveChanges(); return(true); } return(false); } }
// 11.Add category public Category CreateCategory(string name, string description) { using (var db = new NorthwindContex()) { if (name.Length <= 15) { int maxId = db.Categories.OrderByDescending(u => u.Id).FirstOrDefault().Id; Category category = new Category { Id = maxId + 1, CategoryName = name, CategoryDescription = description }; db.Categories.Add(category); db.SaveChanges(); return(category); } return(null); } }