static void Main(string[] args) { Console.WriteLine("Hello World Entity Framework Core!"); using (var context = new VideoGamesDatabaseContext()) { // Migrate the database context.Database.Migrate(); // Verify that all previous data is kept Console.WriteLine("Current database content"); foreach (var videoGame in context.VideoGames.ToList()) { Console.WriteLine($"{videoGame.Title} - {videoGame.Platform} - {videoGame.ReleaseYear}"); } // Add a video game with its release year filled context.VideoGames.Add(new VideoGame { Title = "Final Fantasy XV", Platform = "PS4", ReleaseYear = 2016 }); context.SaveChanges(); // Fetch all video games Console.WriteLine("Current database content"); foreach (var videoGame in context.VideoGames.ToList()) { Console.WriteLine($"{videoGame.Title} - {videoGame.Platform} - {videoGame.ReleaseYear}"); } } }
static void Main(string[] args) { Console.WriteLine("Hello World Entity Framework Core!"); using (var context = new VideoGamesDatabaseContext()) { // Migrate the database context.Database.Migrate(); //delete all previous entitiies foreach (var videoGame in context.VideoGames) { context.Remove(videoGame); } context.SaveChanges(); } var results = new List <ExecutionResult>(); results.Add(MeasureDelete("All 500 entities", 500, vg => true)); results.Add(MeasureDelete("All 500 entities 2", 500, vg => true)); results.Add(MeasureDelete("500 entities, year > 250", 500, vg => vg.ReleaseYear > 250)); results.Add(MeasureDelete("All 2000 entities", 2000, vg => true)); results.Add(MeasureDelete("2000 entities, year > 1000", 2000, vg => vg.ReleaseYear > 1000)); results.Add(MeasureDelete("All 10000 entities", 10000, vg => vg.ReleaseYear > 1000)); results.Add(MeasureDelete("10000 entities, year > 5000", 10000, vg => vg.ReleaseYear > 5000)); Console.WriteLine("No batch(ms) Batch(ms) Title"); Console.WriteLine(string.Join("\n", results)); }
static void Main(string[] args) { Console.WriteLine("Hello World Entity Framework Core!"); using (var context = new VideoGamesDatabaseContext()) { //The line below clears and resets the databse. context.Database.EnsureDeleted(); // Create the database if it does not exist context.Database.EnsureCreated(); // Add some video games. //Note that the Id field is autoincremented by default context.VideoGames.Add(new VideoGame { Title = "Persona 5", Platform = "PS4" }); var SG = new VideoGame(); SG.Title = "Steins's Gate"; SG.Platform = "PSVita"; context.VideoGames.Add(SG); //Commit changes by calling save changes context.SaveChanges(); // Fetch all video games Console.WriteLine("Current database content"); foreach (var videoGame in context.VideoGames.ToList()) { Console.WriteLine($"{videoGame.Title} - {videoGame.Platform}"); } // Fetch all PS4 games var ps4Games = from v in context.VideoGames where v.Platform == "PS4" select v; Console.WriteLine("PS4 Games"); foreach (var videoGame in ps4Games) { Console.WriteLine($"{videoGame.Title} - {videoGame.Platform}"); } //delete ps4 games Console.WriteLine("Deleting PS4 Games"); context.VideoGames.RemoveRange(ps4Games); //Do not forget to commit changes by calling save changes context.SaveChanges(); Console.WriteLine("Current database content"); foreach (var videoGame in context.VideoGames) { Console.WriteLine($"{videoGame.Title} - {videoGame.Platform}"); } } }
static ExecutionResult MeasureDelete(string title, int count, Func <VideoGame, bool> predicate) { TimeSpan duration; DateTime start; var result = new ExecutionResult(); result.Title = title; FillDatabase(count); Console.WriteLine("Start classic delete"); using (var context = new VideoGamesDatabaseContext()) { start = DateTime.Now; //delete all previous entitiies foreach (var videoGame in context.VideoGames.Where(predicate)) { context.Remove(videoGame); } context.SaveChanges(); duration = DateTime.Now - start; } result.ClassicTime = duration.TotalMilliseconds; Console.WriteLine($"Finished classic delete time {duration.Ticks}"); FillDatabase(count); Console.WriteLine("Starting batch delete"); using (var context = new VideoGamesDatabaseContext()) { start = DateTime.Now; var res = context.VideoGames.Where(vg => predicate(vg)).Delete(x => x.BatchSize = 500); //no need to save context duration = DateTime.Now - start; } result.BatchTime = duration.TotalMilliseconds; Console.WriteLine($"Finished batch delete time {duration.Ticks}"); return(result); }
static void FillDatabase(int count) { Console.WriteLine("start adding"); using (var context = new VideoGamesDatabaseContext()) { for (int i = 0; i < count; i++) { var vg = new VideoGame { Platform = "PS4", Title = $"Game {i}", ReleaseYear = i }; context.Add(vg); } context.SaveChanges(); } Console.WriteLine("Finished adding"); }