public static List <SuperHero> RetrieveAllSuperHeroesAndTheirRelatedData() { Program.WriteHighlightedLine("Starting the process to retrieve all superheroes - and all their related data - from the Database"); List <SuperHero> listOfHeroes; using (var context = new SuperHeroDbContext()) { // Load All Heroes var dbSetOfHeroes = context.SuperHeroes // We don't need to track changes, as I'll only ever read from this request. .AsNoTracking() // Including their RealIdentity .Include(hero => hero.RealIdentity) // Including their Quotes .Include(hero => hero.Quotes) // Including their SuperHeroBattles .Include(hero => hero.SuperHeroBattles) // Inside of which, include the battle .ThenInclude(shb => shb.Battle) // Inside of which, include the BattleLog .ThenInclude(btl => btl.BattleLog) // Inside of which, include the BattleEvents .ThenInclude(btlLog => btlLog.BattleEvents); listOfHeroes = dbSetOfHeroes.ToList(); } Program.WriteHighlightedLine($"Successfully retrieved all {listOfHeroes.Count} superheroes from database!"); return(listOfHeroes); }
private static void AddRandomData() { var RNG = new Random(); // For each battle, what is the chance that the hero was apart of it. const int heroChanceToBeInABattle = 80; // Out of 100 List <SuperHero> heroes = HeroCreator.CreateRandom(10, RNG); List <Battle> battles = BattleCreator.CreateRandom(10); WriteHighlightedLine("Assigning battles to heroes.. Randomly!"); heroes.ForEach(hero => { battles.ForEach(battle => { if (RNG.Next(1, 101) < 80) { hero.SuperHeroBattles.Add(SuperHeroBattle.CreateInstance(hero, battle)); } }); }); WriteHighlightedLine("Assignment succeeded"); using (var context = new SuperHeroDbContext()) { context.SuperHeroes.AddRange(heroes); context.Battles.AddRange(battles); context.SaveChanges(); } }
public static void AddSomeBattles() { Program.WriteHighlightedLine("Starting the process of adding multiple battle to the database!"); const int numberOfBattles = 10; const int numberOfBattleEvents = 10; using (var context = new SuperHeroDbContext()) { for (var i = 0; i < numberOfBattles; i++) { var name = $"Battle #{i}"; var description = $"Description for battle #{i}"; var isBrutal = (i % 2 == 0); var battleLog = BattleLog.CreateInstance("Battle Log Of: \n " + name, numberOfBattleEvents); var startDate = DateTime.Now; var endDate = DateTime.Now.AddDays(-1); var newBattle = Battle.CreateInstance(name, description, isBrutal, startDate); newBattle.EndDate = endDate; newBattle.BattleLog = battleLog; context.Battles.Add(newBattle); } context.SaveChanges(); } Program.WriteHighlightedLine("Finalized the process of adding a battle to the database!"); }
public static void AddOneBattle() { // Console.WriteLine("Starting the process of adding a battle to the database!"); const string battleName = "An Added Battle"; const string battleDescription = "Was added via the 'AddOneBattle' function in the BattleCreator Class"; const bool isBrutal = false; var today = DateTime.Now; var yesterday = today.AddDays(-1); const int numberOfBattleEvents = 10; var newBattleLog = BattleLog.CreateInstance("Battle Log Of: \n " + battleName, numberOfBattleEvents); var newBattle = Battle.CreateInstance(battleName, battleDescription, isBrutal, yesterday); newBattle.EndDate = today; newBattle.BattleLog = newBattleLog; using (var context = new SuperHeroDbContext()) { context.Battles.Add(newBattle); context.SaveChanges(); } // Console.WriteLine("Finalized the process of adding a battle to the database!"); }
private static void ClearDataBase() { WriteHighlightedLine("Starting the process of removing all battles and superheroes, and hopefully (by the way of cascading deletion) deleted all other entities as well"); using (var context = new SuperHeroDbContext()) { context.Battles.RemoveRange(context.Battles.Where(b => b.Id >= 0)); context.SuperHeroes.RemoveRange(context.SuperHeroes.Where(b => b.Id >= 0)); context.SaveChanges(); } WriteHighlightedLine("Sucessfully cleared the database!"); }
public static void AddOneSuperHero() { Console.WriteLine("Creating a new Hero"); var newHero = SuperHero.CreateInstance("FoxBear"); using (var context = new SuperHeroDbContext()) { context.SuperHeroes.Add(newHero); context.SaveChanges(); } Console.WriteLine("Successfully created a new Hero!"); }
public static List <SuperHero> RetrieveAllSuperHeroes_OrderByName() { Program.WriteHighlightedLine("Starting the process of retrieving all superheroes - ordered by name"); List <SuperHero> listOfHeroes; using (var context = new SuperHeroDbContext()) { var dbSetOfHeroes = context.SuperHeroes.AsNoTracking().OrderBy(hero => hero.Name); listOfHeroes = dbSetOfHeroes.ToList(); } Program.WriteHighlightedLine($"Successfully retrieved all {listOfHeroes.Count} superheroes from database! - ordered by name"); return(listOfHeroes); }
public static void AddSomeSuperHeroes(int heroAmount) { Console.WriteLine("Creating some heroes"); var someNewHeroes = new Domain.SuperHero[heroAmount]; for (var i = 0; i < heroAmount; i++) { someNewHeroes[i] = SuperHero.CreateInstance("Hero" + i); } using (var context = new SuperHeroDbContext()) { context.SuperHeroes.AddRange(someNewHeroes); context.SaveChanges(); } Console.WriteLine("Successfully created some new heroes!"); }
private static void AddOneSuperHeroWithRelatedData() { var hero = HeroCreator.CreateAllMightFromBNHA(); var battle = BattleCreator.CreateRandom(); var superHeroBattle = SuperHeroBattle.CreateInstance(hero, battle); hero.SuperHeroBattles.Add(superHeroBattle); battle.SuperHeroBattles.Add(superHeroBattle); using (var context = new SuperHeroDbContext()) { context.Battles.Add(battle); context.SuperHeroes.Add(hero); context.SaveChanges(); } }
public static void FindSuperHeroWithRealName(string realName) { Program.WriteHighlightedLine($"Starting the process of finding a superhero with the real name of {realName}"); SuperHero hero; using (var context = new SuperHeroDbContext()) { hero = context.RealIdentities // In order to get the values of the SuperHero, I first need to retrieve the superhero. .Include(ri => ri.SuperHero) .First(ri => ri.RealName == realName).SuperHero; } if (hero == null) { Program.WriteHighlightedLine($"No SuperHero with realName {realName} exists."); } else { Program.WriteHighlightedLine($"The SuperHero {hero.Name} has the realName {realName}.. HOW DID YOU KNOW?"); } }