public static async Task PreparationAsync() { using (var context = new MenusContext()) { bool deleted = await context.Database.EnsureDeletedAsync(); string deletedText = deleted ? "deleted" : "does not exist"; WriteLine($"database {deletedText}"); bool created = await context.Database.EnsureCreatedAsync(); string createdText = created ? "created" : "already exists"; WriteLine($"database {createdText}"); var card = new MenuCard() { Title = "Meat" }; var m1 = new Menu { MenuCard = card, Text = "Wiener Schnitzel", Price = 12.90m }; var m2 = new Menu { MenuCard = card, Text = "Goulash", Price = 8.80m }; card.Menus.AddRange(new Menu[] { m1, m2 }); context.MenuCards.Add(card); int records = await context.SaveChangesAsync(); WriteLine($"{records} records added"); } }
static void Main(string[] args) { MenusContext context = new MenusContext(); bool created = context.Database.EnsureCreated(); Console.WriteLine(created); }
private static void ObjectTracking() { WriteLine(nameof(ObjectTracking)); using (var context = new MenusContext()) { context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; var m1 = (from m in context.Menus.AsNoTracking() where m.Text.StartsWith("Con") select m).FirstOrDefault(); var m2 = (from m in context.Menus where m.Text.Contains("(") select m).FirstOrDefault(); if (object.ReferenceEquals(m1, m2)) { WriteLine("the same object"); } else { WriteLine("not the same"); } ShowState(context); } WriteLine(); }
public static void DeleteDatabase() { using (var context = new MenusContext()) { bool deleted = context.Database.EnsureDeleted(); } }
private static async Task AddTwoRecordsWithTwoTxAsync() { WriteLine(nameof(AddTwoRecordsWithTwoTxAsync)); try { using (var context = new MenusContext()) { WriteLine("adding two records with two transactions to the database. One record should be written, the other not...."); var card = context.MenuCards.First(); var m1 = new Menu { MenuCardId = card.MenuCardId, Text = "added", Price = 99.99m }; context.Menus.Add(m1); int records = await context.SaveChangesAsync(); WriteLine($"{records} records added"); int hightestCardId = await context.MenuCards.MaxAsync(c => c.MenuCardId); var mInvalid = new Menu { MenuCardId = ++hightestCardId, Text = "invalid", Price = 999.99m }; context.Menus.Add(mInvalid); records = await context.SaveChangesAsync(); WriteLine($"{records} records added"); } } catch (DbUpdateException ex) { WriteLine($"{ex.Message}"); WriteLine($"{ex?.InnerException.Message}"); } WriteLine(); }
public static void CreateDatabase() { using (var context = new MenusContext()) { bool created = context.Database.EnsureCreated(); } }
private static async Task AddTwoRecordsWithOneTxAsync() { WriteLine(nameof(AddTwoRecordsWithOneTxAsync)); try { using (var context = new MenusContext()) { var card = context.MenuCards.First(); var m1 = new Menu { MenuCardId = card.MenuCardId, Text = "added", Price = 99.99m }; int hightestCardId = await context.MenuCards.MaxAsync(c => c.MenuCardId); var mInvalid = new Menu { MenuCardId = ++hightestCardId, Text = "invalid", Price = 999.99m }; context.Menus.AddRange(m1, mInvalid); WriteLine("trying to add one invalid record to the database, this should fail..."); int records = await context.SaveChangesAsync(); WriteLine($"{records} records added"); } } catch (DbUpdateException ex) { WriteLine($"{ex.Message}"); WriteLine($"{ex?.InnerException.Message}"); } WriteLine(); }
public static void ShowState(MenusContext context) { foreach (EntityEntry entry in context.ChangeTracker.Entries()) { Console.WriteLine($"type: {entry.Entity.GetType().Name}, state: {entry.State}," + $" {entry.Entity}"); } }
private static async Task<Menu> GetMenuAsync() { using (var context = new MenusContext()) { Menu menu = await context.Menus .Skip(2) .FirstOrDefaultAsync(); return menu; } }
private static async Task CreateDatabaseAsync() { using (var context = new MenusContext()) { bool created = await context.Database.EnsureCreatedAsync(); string createdText = created ? "created" : "already exists"; WriteLine($"database {createdText}"); } }
private static async Task <Menu> GetMenuAsync() { using (var context = new MenusContext()) { Menu menu = await context.Menus .Skip(2) .FirstOrDefaultAsync(); return(menu); } }
private static void InitialFill() { MenuCard[] cards = new MenuCard[] { new MenuCard { } }; using (var context = new MenusContext()) { } }
private static void UpdateUntracked(Menu m) { using (var context = new MenusContext()) { ShowState(context); // EntityEntry<Menu> entry = context.Menus.Attach(m); // entry.State = EntityState.Modified; context.Menus.Update(m); ShowState(context); context.SaveChanges(); } }
public static void DeleteDatabase() { Console.Write("Delete the database? "); string input = Console.ReadLine(); if (input.ToLower() == "y") { using (var context = new MenusContext()) { bool deleted = context.Database.EnsureDeleted(); } } }
private static async Task UpdateUntrackedAsync(Menu m) { using (var context = new MenusContext()) { ShowState(context); //EntityEntry<Menu> entry = context.Menus.Attach(m); //entry.State = EntityState.Modified; context.Menus.Update(m); ShowState(context); await context.SaveChangesAsync(); } }
private static void AddRecords() { Console.WriteLine(nameof(AddRecords)); try { using (var context = new MenusContext()) { var soupCard = new MenuCard(); Menu[] soups = { new Menu { Text = "Consommé Célestine (with shredded pancake)", Price = 4.8m, MenuCard = soupCard }, new Menu { Text = "Baked Potato Soup", Price = 4.8m, MenuCard = soupCard }, new Menu { Text = "Cheddar Broccoli Soup", Price = 4.8m, MenuCard = soupCard }, }; soupCard.Title = "Soups"; soupCard.Menus.AddRange(soups); context.MenuCards.Add(soupCard); ShowState(context); int records = context.SaveChanges(); Console.WriteLine($"{records} added"); Console.WriteLine(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine(); }
private static void UpdateRecords() { Console.WriteLine(nameof(UpdateRecords)); using (var context = new MenusContext()) { Menu menu = context.Menus .Skip(1) .FirstOrDefault(); ShowState(context); menu.Price += 0.2m; ShowState(context); int records = context.SaveChanges(); Console.WriteLine($"{records} updated"); ShowState(context); } Console.WriteLine(); }
private static void ChangeUntracked() { Menu GetMenu() { using (var context = new MenusContext()) { Menu menu = context.Menus .Skip(2) .FirstOrDefault(); return(menu); } } Menu m = GetMenu(); m.Price += 0.7m; UpdateUntracked(m); }
private static async Task UpdateRecordsAsync() { WriteLine(nameof(UpdateRecordsAsync)); using (var context = new MenusContext()) { Menu menu = await context.Menus .Skip(1) .FirstOrDefaultAsync(); ShowState(context); menu.Price += 0.2m; ShowState(context); int records = await context.SaveChangesAsync(); WriteLine($"{records} updated"); ShowState(context); } WriteLine(); }
private static void AddHundredRecords() { Console.WriteLine(nameof(AddHundredRecords)); using (var context = new MenusContext()) { var card = context.MenuCards.FirstOrDefault(); if (card != null) { var menus = Enumerable.Range(1, 100).Select(x => new Menu { MenuCard = card, Text = $"menu {x}", Price = 9.9m }); context.Menus.AddRange(menus); int records = context.SaveChanges(); Console.WriteLine($"{records} added"); } } Console.WriteLine(); }
private static void ObjectTracking() { Console.WriteLine(nameof(ObjectTracking)); using (var context = new MenusContext()) { var m1 = (from m in context.Menus where m.Text.StartsWith("Con") select m).FirstOrDefault(); var m2 = (from m in context.Menus where m.Text.Contains("(") select m).FirstOrDefault(); if (object.ReferenceEquals(m1, m2)) { Console.WriteLine("the same object"); } else { Console.WriteLine("not the same"); } ShowState(context); } Console.WriteLine(); }
private static void AddHundredRecords() { Console.WriteLine(nameof(AddHundredRecords)); using (var context = new MenusContext()) { var card = context.MenuCards.FirstOrDefault(); if (card != null) { var menus = Enumerable.Range(1, 100).Select(x => new Menu { MenuCard = card, Text = $"menu {x}", Price = 9.9m }); context.Menus.AddRange(menus); Stopwatch stopwatch = Stopwatch.StartNew(); int records = context.SaveChanges(); stopwatch.Stop(); Console.WriteLine($"{records} records added after {stopwatch.ElapsedMilliseconds} milliseconds"); } } Console.WriteLine(); }
private static async Task TwoSaveChangesWithOneTxAsync() { WriteLine(nameof(TwoSaveChangesWithOneTxAsync)); IDbContextTransaction tx = null; try { using (var context = new MenusContext()) using (tx = await context.Database.BeginTransactionAsync()) { WriteLine("using one explicit transaction, writing should roll back..."); var card = context.MenuCards.First(); var m1 = new Menu { MenuCardId = card.MenuCardId, Text = "added with explicit tx", Price = 99.99m }; context.Menus.Add(m1); int records = await context.SaveChangesAsync(); WriteLine($"{records} records added"); int hightestCardId = await context.MenuCards.MaxAsync(c => c.MenuCardId); var mInvalid = new Menu { MenuCardId = ++hightestCardId, Text = "invalid", Price = 999.99m }; context.Menus.Add(mInvalid); records = await context.SaveChangesAsync(); WriteLine($"{records} records added"); tx.Commit(); } } catch (DbUpdateException ex) { WriteLine($"{ex.Message}"); WriteLine($"{ex?.InnerException.Message}"); WriteLine("rolling back..."); tx.Rollback(); } WriteLine(); }
public static void ShowState(MenusContext context) { foreach (EntityEntry entry in context.ChangeTracker.Entries()) { WriteLine($"type: {entry.Entity.GetType().Name}, state: {entry.State}, {entry.Entity}"); } WriteLine(); }
private static async Task AddRecordsAsync() { WriteLine(nameof(AddRecordsAsync)); try { using (var context = new MenusContext()) { var soupCard = new MenuCard(); Menu[] soups = { new Menu { Text = "Consommé Célestine (with shredded pancake)", Price = 4.8m, MenuCard =soupCard}, new Menu { Text = "Baked Potato Soup", Price = 4.8m, MenuCard = soupCard }, new Menu { Text = "Cheddar Broccoli Soup", Price = 4.8m, MenuCard = soupCard }, }; soupCard.Title = "Soups"; soupCard.Menus.AddRange(soups); context.MenuCards.Add(soupCard); ShowState(context); int records = await context.SaveChangesAsync(); WriteLine($"{records} added"); WriteLine(); } } catch (Exception ex) { WriteLine(ex.Message); } WriteLine(); }