public async Task <IActionResult> PutPokemon(long id, Pokemon pokemon) { if (id != pokemon.Id) { return(BadRequest()); } _context.Entry(pokemon).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PokemonExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
private static void Delete() { Console.Write("enter name pokemon Delete: "); str = Console.ReadLine(); var pokemon = context.Pokemons.Where(s => s.Name == str).ToList(); var pokemon1 = context.Pokemons.FirstOrDefault(s => s.Name == str); if (pokemon.Count > 0) { context.Pokemons.RemoveRange(pokemon); context.SaveChanges(); Console.WriteLine($"State: {context.Entry(pokemon).State}"); } else { Console.WriteLine("Record does not exist in the database"); } }
public ActionResult Edit([Bind(Include = "PokemonId,DefaultImage,Name,Height,Weight")] Pokedata pokedata) { if (ModelState.IsValid) { db.Entry(pokedata).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(pokedata)); }
public async Task <IActionResult> PutPokemon([FromRoute] int id, [FromBody] Pokemon pokemon) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != pokemon.ID) { return(BadRequest()); } _context.Entry(pokemon).State = EntityState.Modified; try { await _context.SaveChangesAsync(); return(Ok(pokemon)); } catch (DbUpdateConcurrencyException) { if (!PokemonExists(id)) { ModelState.AddModelError("", "Concurrency Error: Pokemon has been removed."); return(BadRequest(ModelState)); } else { ModelState.AddModelError("", "Concurrency Error: Pokemon has been updated by another user. Cancel and trying editing the record again."); return(BadRequest(ModelState)); } } catch (DbUpdateException dex) { if (dex.InnerException.Message.Contains("IX_")) { ModelState.AddModelError("", "Unable to save changes: Duplicate Number, trying again."); return(BadRequest(ModelState)); } else { ModelState.AddModelError("", "Unable to save changes to the database. Try refreshing and trying again."); return(BadRequest(ModelState)); } } catch (ArgumentOutOfRangeException) { ModelState.AddModelError("", "Unable to save changes, Number is out of range."); return(BadRequest(ModelState)); } }
public async Task <IActionResult> PutTypes([FromRoute] int id, [FromBody] Types types) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != types.ID) { return(BadRequest()); } _context.Entry(types).State = EntityState.Modified; try { await _context.SaveChangesAsync(); return(Ok(types)); } catch (DbUpdateConcurrencyException) { if (!TypesExists(id)) { ModelState.AddModelError("", "Concurrency Error: Type has been removed."); return(BadRequest(ModelState)); } else { ModelState.AddModelError("", "Concurrency Error: Type has been updated by another user. Cancel and trying editing the record again."); return(BadRequest(ModelState)); } } catch (DbUpdateException dex) { if (dex.InnerException.Message.Contains("IX_")) { ModelState.AddModelError("", "Unable to save changes: Duplicate Type Name."); return(BadRequest(ModelState)); } else { ModelState.AddModelError("", "Unable to save changes to the database. Try refreshing and trying again."); return(BadRequest(ModelState)); } } }