public ActionResult Create([Bind(Include = "Id,Name,Description,Rating,Picture")] Pokemon pokemon) { if (ModelState.IsValid) { db.Pokemans.Add(pokemon); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(pokemon)); }
public IActionResult SaveChanges(FavoritePokemon updatedName) //saves changes when user adds nickname to favorited pokemon { FavoritePokemon pokemonName = _pokemonContext.FavoritePokemon.Find(updatedName.Id); pokemonName.Nickname = updatedName.Nickname; _pokemonContext.Entry(pokemonName).State = Microsoft.EntityFrameworkCore.EntityState.Modified; _pokemonContext.Update(pokemonName); _pokemonContext.SaveChanges(); return(RedirectToAction("Favorites")); }
public ActionResult Create(Pokemon pokemon, int PokeTypeId, int TrainerId) { _db.Pokemons.Add(pokemon); if (PokeTypeId != 0) { _db.Pokedex.Add(new Pokedex() { PokeTypeId = PokeTypeId, PokemonId = pokemon.PokemonId }); } _db.SaveChanges(); return(RedirectToAction("Index")); }
// add some new pokemon (whether hardcoded, or user input) private static void AddNewPokemon(DbContextOptions <PokemonDbContext> options) { using (var dbContext = new PokemonDbContext(options)) { if (!dbContext.Pokemon.Any(x => x.Name == "Charmander")) { // ^ is converted to a SELECT/FROM/WHERE, runs in the DB, not in the C#. var newPokemon = new Pokemon { Name = "Charmander", Height = 36, Type = dbContext.Type.FirstOrDefault(x => x.Name == "Fire") ?? new Type { Name = "Fire" } }; dbContext.Pokemon.Add(newPokemon); dbContext.SaveChanges(); // add the new pokemon and also the new type. } else { Console.WriteLine("Charmander already exists... doing nothing."); } } }
static void AddNewPokemon(PokemonDbContext context) { // check if already exists if (context.Pokemon.Any(p => p.Name == "Charmander")) { Console.WriteLine("Charmander already exists; not adding again"); return; } var charmander = new Pokemon { Name = "Charmander", Height = 6, Weight = 85 }; var fireType = context.Type.FirstOrDefault(t => t.Name == "Fire") ?? new DataAccess.Entities.Type { Name = "Fire" }; charmander.PokemonType.Add(new PokemonType { Type = fireType }); context.Pokemon.Add(charmander); // there are foreign key properties I have not connected up - that's fine // there are navigation properties i have not connected up - that's also fine context.SaveChanges(); }
public JsonResult AfterInput(Pokemon pokes) { PokemonDbContext database = new PokemonDbContext(); database.Pokemans.Add(pokes); database.SaveChanges(); return(Json(pokes)); }
static void AddNewPokemon(PokemonDbContext context) { using (var db = new PokemonDbContext()) { db.Add(new Pokemon { "Coltron" }); db.SaveChanges(); } }
//public IActionResult Add() //{ // return View(); //} public IActionResult Add(string pokemon) { string poke = pokemon.Trim().ToLower(); PokemonRoot p = pk.GetPokemon(poke);//Allows addition of other properties to the SQL table FavPokemon favPokemon = new FavPokemon(); favPokemon.Image = p.sprites.front_default; favPokemon.Type1 = p.types[0].type.name; if (p.types.Length < 2) { favPokemon.Type2 = ""; } else { favPokemon.Type2 = ", " + p.types[1].type.name; } string url = $@"https://pokeapi.co/api/v2/pokemon/{pokemon}/"; List <FavPokemon> favPokeList = _PokemonDB.FavPokemons.Where(x => x.UserId == User.FindFirst(ClaimTypes.NameIdentifier).Value).ToList(); if (favPokeList.Any(x => pokemon == x.Name)) { TempData["faverror"] = "This pokemon is already in your favorites"; return(RedirectToAction("Index")); } else { favPokemon.Name = pokemon; favPokemon.Url = url; favPokemon.UserId = User.FindFirst(ClaimTypes.NameIdentifier).Value; TempData.Remove("faverror"); _PokemonDB.FavPokemons.Add(favPokemon); _PokemonDB.SaveChanges(); return(RedirectToAction("Index")); } }
public async Task <IActionResult> Post(Pokemon poke) { if (ModelState.IsValid) { _db.Pokemon.Add(poke); _db.SaveChanges(); //add and save first return(await Task.FromResult(Ok(poke))); } return(await Task.FromResult(NotFound(poke))); }
public IActionResult Registro(Pokemon p) { if (ModelState.IsValid) { _context.Add(p); _context.SaveChanges(); return(RedirectToAction("Index")); } else { return(View(p)); } }
public static void InitializeDatabase(PokemonDbContext db) { var admin = InitializeAdmin(); var abilities = InitializeAbilities(); var pokemon = InitializePokemon(); db.Admins.Add(admin); db.Abilities.AddRange(abilities); db.Pokemons.AddRange(pokemon); db.SaveChanges(); }
private static void DeleteSomePokemon(DbContextOptions <PokemonDbContext> options) { using (var dbContext = new PokemonDbContext(options)) { if (dbContext.Pokemon.FirstOrDefault(x => x.Name == "Charmander") is Pokemon charmander) { dbContext.Pokemon.Remove(charmander); dbContext.SaveChanges(); } else { Console.WriteLine("Charmander doesn't exist, not removing."); } } }
static void EditAPokemon(PokemonDbContext context) { // check if already exists var charmander = context.Pokemon.FirstOrDefault(p => p.Name == "Charmander"); // if this dbcontext instance is tracking any objects already, you will see them // connected to other objects even if you didn't not call include THAT time. if (charmander is null) { Console.WriteLine("Charmander does not exist; not editing"); return; } charmander.Height += 100; context.SaveChanges(); }
static void DeleteAPokemon(PokemonDbContext context) { // check if already exists var charmander = context.Pokemon.FirstOrDefault(p => p.Name == "charmander"); //IQueryable<int> queryableOfHeights = context.Pokemon.Where(p => p.Name.Length > 6).Select(p => p.Height); //// IQueryable also uses deferred execution -- no SQL commands have been executed yet //queryableOfHeights.ToList(); // if this dbcontext instance is tracking any objects already, you will see them // connected to other objects even if you didn't not call include THAT time. if (charmander is null) { Console.WriteLine("Charmander does not exist; not deleting"); return; } context.Pokemon.Remove(charmander); context.SaveChanges(); }
private static void EditSomePokemon(DbContextOptions <PokemonDbContext> options) { // in between dbcontext creation and each savechanges call is a transaction using (var dbContext = new PokemonDbContext(options)) { // EF "tracks" the objects you pull out of it. Pokemon charmander = dbContext.Pokemon.Include(x => x.Type) .First(x => x.Name == "Charmander"); Type grass = dbContext.Type.First(x => x.Name == "Grass"); Type fire = dbContext.Type.First(x => x.Name == "Fire"); if (charmander.Type == fire) { charmander.Type = grass; } else { charmander.Type = fire; } dbContext.SaveChanges(); } }
public ActionResult Create(Trainer trainer) { _db.Trainers.Add(trainer); _db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Create(PokeType pokeType) { _db.PokeTypes.Add(pokeType); _db.SaveChanges(); return(RedirectToAction("Index")); }
public IActionResult Add(PokemonModel newPkmn) { _context.pokemon.Add(newPkmn); _context.SaveChanges(); return(RedirectToAction("Index")); }