public IActionResult New([FromBody] Fruit fruit) { JsonResponse <Fruit> jsonResponse; if (!ModelState.IsValid) { return(BadRequest()); } var fruitEntity = _context.Fruits.SingleOrDefault(f => f.No == fruit.No); if (fruitEntity is null) { fruitEntity = new FruitEntity() { No = fruit.No, Description = fruit.Description }; _context.Fruits.Add(fruitEntity); _context.SaveChanges(); fruit.Id = fruitEntity.Id; jsonResponse = JsonResponse <Fruit> .Success(fruit); } else { jsonResponse = JsonResponse <Fruit> .Failure("Fruit already exists"); } return(Ok(jsonResponse)); }
public ActionResult Create(FruitModel fruitmodel) { if (ModelState.IsValid) { db.Fruits.Add(fruitmodel); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(fruitmodel)); }
private void buttonSaveChanges_Click(object sender, EventArgs e) { //NOTICE we do not loop over grid rows and pull data out of cells and do //sql statement! The grid is only there to provide a UI. It //is set up (data bound) to keep our data source in sync //with the changes to the grid. All we need to do is save //changes on the context and what we see in the grid is what //we will get in the database. ctx.SaveChanges(); }
private void AddFruitsIfDatabaseIsEmpty() { using (var context = new FruitContext()) { if (!context.Fruits.Any()) { var fruits = new List <Fruit> { new Fruit { Name = "Apple" }, new Fruit { Name = "Banana" }, new Fruit { Name = "Orange" } }; fruits.ForEach(s => context.Fruits.Add(s)); context.SaveChanges(); } } }
public void Post([FromBody] Fruit fruit) { _context.Fruits.Add(fruit); _context.SaveChanges(); }