public IHttpActionResult Add(FoodMenu foodMenu) { if (foodMenu == null || foodMenu.FoodItemsId.Count == 0) { return(BadRequest("can't be null")); } if (foodMenu.Id != 0) { var menuInDb = _context.FoodMenus.Include(f => f.FoodItems).SingleOrDefault(m => m.Id == foodMenu.Id); if (menuInDb == null) { return(BadRequest("null value")); } menuInDb.FoodItems = new List <FoodItem>(); foreach (var id in foodMenu.FoodItemsId) { menuInDb.FoodItems.Add(_context.FoodItems.SingleOrDefault(f => f.Id == id)); } menuInDb.ServicePrice = foodMenu.ServicePrice; menuInDb.CalFullPrice(); menuInDb.Name = foodMenu.Name; menuInDb.FoodItemsId = foodMenu.FoodItemsId; _context.SaveChanges(); return(Ok(menuInDb)); } if (_context.FoodMenus.Select(f => f.Name).Contains(foodMenu.Name)) { return(BadRequest("Menu Already Exist")); } ValidItem(foodMenu); foodMenu.CalFullPrice(); _context.FoodMenus.Add(foodMenu); _context.SaveChanges(); return(Created(new Uri(Request.RequestUri + "/" + foodMenu.Id), foodMenu)); }