public ActionResult SuperheroEdit(int?id) { SuperheroContext db = new SuperheroContext(); Superhero heroEdit = db.Superhero.SingleOrDefault(identity => identity.Id == id); return(View(heroEdit)); }
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 SuperheroCreate(int?id) { SuperheroContext db = new SuperheroContext(); Superhero createHero = db.Superhero.SingleOrDefault(identity => identity.Id == 0); return(View(createHero)); }
public ActionResult SuperheroCreate(Superhero superhero) { SuperheroContext db = new SuperheroContext(); db.Superhero.Add(superhero); db.SaveChanges(); return(RedirectToAction("SuperheroList")); }
public ActionResult SuperheroEdit(Superhero superhero) { SuperheroContext db = new SuperheroContext(); db.Entry(superhero).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("SuperheroList")); }
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 SuperheroRepositoryTests() { _connection = new SqliteConnection("DataSource=:memory:"); _connection.Open(); var builder = new DbContextOptionsBuilder <SuperheroContext>().UseSqlite(_connection); _context = new SuperheroTestContext(builder.Options); _context.Database.EnsureCreated(); _repository = new SuperheroRepository(_context); }
public ActionResult SuperheroDetails(int?id) { SuperheroContext db = new SuperheroContext(); Superhero details = db.Superhero.SingleOrDefault(identity => identity.Id == id); if (details == null) { return(HttpNotFound()); } return(View(details)); }
public void Test() { using var connection = new SqliteConnection("Filename=:memory:"); connection.Open(); var builder = new DbContextOptionsBuilder <SuperheroContext>().UseSqlite(connection); using var context = new SuperheroContext(builder.Options); context.Database.EnsureCreated(); context.GenerateTestData(); Assert.Equal(2, context.Superheroes.Count()); }
public SuperheroRepositoryTests() { // Arrange _connection = new SqliteConnection("Filename=:memory:"); _connection.Open(); var builder = new DbContextOptionsBuilder <SuperheroContext>().UseSqlite(_connection); _context = new SuperheroContext(builder.Options); _context.Database.EnsureCreated(); _context.GenerateTestData(); _repository = new SuperheroRepository(_context); }
public void Update_given_no_entity_returns_NotFound() { var builder = new DbContextOptionsBuilder <SuperheroContext>().UseInMemoryDatabase(nameof(Update_given_no_entity_returns_NotFound)); var context = new SuperheroContext(builder.Options); var repository = new SuperheroRepository(context); var superhero = new SuperheroUpdateDTO { Id = 42 }; var response = repository.Update(superhero); Assert.Equal(NotFound, response); }
static void Main(string[] args) { using var context = new SuperheroContext(); var repository = new SuperheroRepository(context); foreach (var hero in repository.Read()) { Console.WriteLine($"{hero.Id}: {hero.Name} aka {hero.AlterEgo}"); } Console.Write("Enter superhero id: "); var id = int.Parse(Console.ReadLine()); var superhero = repository.Read(id); Console.WriteLine($"Name: {superhero.Name}"); Console.WriteLine($"Alter Ego: {superhero.AlterEgo}"); Console.WriteLine($"Occupation: {superhero.Occupation}"); Console.WriteLine($"City: {superhero.CityName}"); Console.WriteLine($"Gender: {superhero.Gender}"); Console.WriteLine($"First Appearance: {superhero.FirstAppearance}"); Console.WriteLine($"Powers: {string.Join(", ", superhero.Powers)}"); }
public SuperheroRepository(SuperheroContext context) : base(context) { _connection = context.Database.GetDbConnection(); }
public SuperheroController(SuperheroContext context) { this.context = context; }
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 ActionResult SuperheroList() { SuperheroContext db = new SuperheroContext(); return(View(db.Superhero)); }
public Repository(SuperheroContext context) { Db = context; DbSet = Db.Set <TEntity>(); }
static void Main(string[] args) { var configuration = new ConfigurationBuilder() .AddUserSecrets(typeof(Program).Assembly) .Build(); // var connectionString = configuration.GetConnectionString("ConnectionString"); // Console.WriteLine(connectionString.Substring(0, 10)); // using var connection = new SqlConnection(connectionString); // connection.Open(); // Console.Write("Input search string: "); // var searchString = Console.ReadLine(); // var cmdText = $"SELECT * FROM Characters WHERE Name LIKE '%' + @SearchString + '%'"; // using var command = new SqlCommand(cmdText, connection); // command.Parameters.AddWithValue("@SearchString", searchString); // using var reader = command.ExecuteReader(); // while (reader.Read()) // { // Console.WriteLine(reader["Name"]); // } // RawSqlDemo.Run(connectionString); // var builder = new DbContextOptionsBuilder<FuturamaContext>(); // builder.UseSqlServer(connectionString); // using var context = new FuturamaContext(builder.Options); // foreach (var character in context.Characters) // { // Console.WriteLine(character.Name); // } // var amy = from c in context.Characters // where c.Name == "Amy Wong" // select new { c.Name, Actor = c.Actor.Name }; // amy.Print(); // var hermes = context.Characters.First(h => h.Name.Contains("Conrad")); // context.Characters.Remove(hermes); // context.SaveChanges(); // var hubert = context.Characters.Find(7); // Console.WriteLine(hubert.Name); // hubert.Name = "Hubert Farnsworth"; // context.SaveChanges(); var connectionString = configuration.GetConnectionString("Superheroes"); var builder = new DbContextOptionsBuilder <SuperheroContext>(); builder.UseSqlServer(connectionString) .UseLazyLoadingProxies(); using var context = new SuperheroContext(builder.Options); // var superheroes = from h in context.Superheroes // select new // { // h.Name, // Powers = h.Powers.Select(p => p.Power.Name) // }; // foreach (var hero in superheroes) // { // Console.WriteLine(hero.Name); // hero.Powers.Print(); // } // context.Database.ExecuteSqlRaw("UPDATE Powers SET Name = 'Updated(' + Name + ')'"); var heroes = context.Superheroes; //.Include(c => c.City); foreach (var hero in heroes) { Console.WriteLine(hero.City.Name); } // var heroes = from h in context.Superheroes // orderby h.AlterEgo // select new // { // h.AlterEgo, // h.Name, // City = h.City.Name // }; // heroes.Print(); }
public SuperheroRepository(SuperheroContext context) { _context = context; }