public void Update_given_existing_entity_uses_existing_city() { var builder = new DbContextOptionsBuilder <SuperheroContext>().UseInMemoryDatabase(nameof(Update_given_no_entity_returns_NotFound)); var context = new SuperheroContext(builder.Options); var entity = new Superhero { Name = "Bruce Wayne", AlterEgo = "Batman" }; context.Superheroes.Add(entity); var entityCity = new City { Name = "Metropolis" }; context.Cities.Add(entityCity); context.SaveChanges(); var repository = new SuperheroRepository(context); var superhero = new SuperheroUpdateDTO { Id = entity.Id, Name = "Clark Kent", AlterEgo = "Superman", CityName = "Metropolis" }; var response = repository.Update(superhero); var updated = context.Superheroes.Find(entity.Id); Assert.Equal("Clark Kent", updated.Name); Assert.Equal("Superman", updated.AlterEgo); Assert.Equal(entityCity.Id, updated.CityId); }
public ActionResult SuperheroEdit(Superhero superhero) { SuperheroContext db = new SuperheroContext(); db.Entry(superhero).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("SuperheroList")); }
public ActionResult SuperheroCreate(Superhero superhero) { SuperheroContext db = new SuperheroContext(); db.Superhero.Add(superhero); db.SaveChanges(); return(RedirectToAction("SuperheroList")); }
public (Response response, int superheroId) Create(SuperheroCreateDTO superhero) { var entity = new Superhero { Name = superhero.Name, AlterEgo = superhero.AlterEgo, City = ReadOrCreateCity(superhero.CityName), FirstAppearance = superhero.FirstAppearance, Gender = superhero.Gender, Occupation = superhero.Occupation }; _context.Superheroes.Add(entity); _context.SaveChanges(); return(Created, entity.Id); }
public ActionResult SuperheroDelete(int id) { SuperheroContext db = new SuperheroContext(); Superhero heroDelete = db.Superhero.SingleOrDefault(identity => identity.Id == id); db.Superhero.Remove(heroDelete); db.SaveChanges(); return(RedirectToAction("SuperheroList")); }
public static void GenerateTestData(this SuperheroContext context) { var superman = new Superhero { Name = "Clark Kent", AlterEgo = "Superman", Occupation = "Reporter", Gender = Male, FirstAppearance = 1938, City = new City { Name = "Metropolis" }, Powers = new[] { new SuperheroPower { Power = new Power { Name = "super strength" } }, new SuperheroPower { Power = new Power { Name = "flight" } }, new SuperheroPower { Power = new Power { Name = "invulnerability" } }, new SuperheroPower { Power = new Power { Name = "super speed" } }, new SuperheroPower { Power = new Power { Name = "heat vision" } }, new SuperheroPower { Power = new Power { Name = "freeze breath" } }, new SuperheroPower { Power = new Power { Name = "x-ray vision" } }, new SuperheroPower { Power = new Power { Name = "superhuman hearing" } }, new SuperheroPower { Power = new Power { Name = "healing factor" } } } }; var batman = new Superhero { Name = "Bruce Wayne", AlterEgo = "Batman", Occupation = "CEO of Wayne Enterprises", Gender = Male, FirstAppearance = 1939, City = new City { Name = "Gotham City" }, Powers = new[] { new SuperheroPower { Power = new Power { Name = "exceptional martial artist" } }, new SuperheroPower { Power = new Power { Name = "combat strategy" } }, new SuperheroPower { Power = new Power { Name = "inexhaustible wealth" } }, new SuperheroPower { Power = new Power { Name = "brilliant deductive skills" } }, new SuperheroPower { Power = new Power { Name = "advanced technology" } } } }; context.Superheroes.AddRange(superman, batman); context.SaveChanges(); }
public int SaveChanges() { return(Db.SaveChanges()); }