static void Main(string[] args) { Author existingAuthor; using (var ctx = new EfTestDbContext()) { existingAuthor = new Author() { Name = "First author" }; ctx.Add(existingAuthor); ctx.SaveChanges(); Console.WriteLine($"Saved author ({existingAuthor.Name})"); } using (var ctx = new EfTestDbContext()) { var book1 = new Book() { Name = "First book", Author = existingAuthor }; Console.WriteLine($"Added author ({existingAuthor.Name}) to first book. Author is existing: {ctx.Entry(existingAuthor).IsKeySet}"); ctx.Add(book1); ctx.SaveChanges(); } Console.WriteLine("ALL DONE"); }
public static async void ReplaceOrderTimeOnInternalOrder() { // create internal order InternalOrder internalOrder; using (var dbContext = new EfTestDbContext()) { var repo = new InternalOrderRepository(dbContext); var seq = await repo.NextOrderNumber(); internalOrder = new InternalOrder("W-{seq}"); internalOrder.SetTime(new OrderTime("Test")); await repo.Update(internalOrder); await repo.SaveAsync(); Debug.Assert(internalOrder.ID != Guid.Empty); Debug.Assert(null != internalOrder.OrderTimeID); } // replace order time Guid previousOrderTimeId; using (var dbContext = new EfTestDbContext()) { var repo = new InternalOrderRepository(dbContext); // get untracked InternalOrder var order = await dbContext.InternalOrders.Include(x => x.OrderTime).SingleAsync(x => x.ID == internalOrder.ID); previousOrderTimeId = order.OrderTime.ID; order.SetTime(new OrderTime("Test 1234")); Console.WriteLine($"Previous order time id {previousOrderTimeId}"); Console.WriteLine($"Current order time id {order.OrderTime.ID}"); await repo.Update(order); await Task.Delay(1000); // for sensitive logging to settle Console.WriteLine($"-- Before save changes --"); await repo.SaveAsync(); Console.WriteLine($"Current order time id after save {order.OrderTime.ID}"); } // check if OrderTime was deleted using (var dbContext = new EfTestDbContext()) { var repo = new InternalOrderRepository(dbContext); var order = repo.Get(internalOrder.ID); Debug.Assert(false == dbContext.OrderTimes.Any(x => x.ID == previousOrderTimeId), "OrderTime is still present"); Debug.Assert(null != order.OrderTime); Debug.Assert("Test 1234" == order.OrderTime.Display); } }
static void Main(string[] args) { using (var ctx = new EfTestDbContext()) { ctx.Database.EnsureDeleted(); ctx.Database.EnsureCreated(); } // RemoveOrderTimeFromInternalOrder(); // works // RemoveOrderTimeFromInternalOrderWithTracking(); // breaks - orphan OrderTime // ReplaceOrderTimeOnInternalOrderTrackedEntities(); // breaks - orphan OrderTime ReplaceOrderTimeOnInternalOrder(); // breaks - foreign key violation Console.ReadLine(); Console.WriteLine($"Done"); }
public static async void RemoveOrderTimeFromInternalOrder() { // create internal order InternalOrder internalOrder; using (var dbContext = new EfTestDbContext()) { var repo = new InternalOrderRepository(dbContext); var seq = await repo.NextOrderNumber(); internalOrder = new InternalOrder("W-{seq}"); internalOrder.SetTime(new OrderTime("Test")); await repo.Update(internalOrder); await repo.SaveAsync(); Debug.Assert(internalOrder.ID != Guid.Empty); Debug.Assert(null != internalOrder.OrderTimeID); } // remove order time Guid previousOrderTimeId; using (var dbContext = new EfTestDbContext()) { var repo = new InternalOrderRepository(dbContext); var order = repo.Get(internalOrder.ID); previousOrderTimeId = order.OrderTime.ID; order.RemoveTime(); Console.WriteLine($"Previous order time id {previousOrderTimeId}"); await repo.Update(order); await repo.SaveAsync(); } // check if OrderTime was deleted using (var dbContext = new EfTestDbContext()) { var order = dbContext.InternalOrders .Include(x => x.OrderTime) .Single(x => x.ID == internalOrder.ID); Debug.Assert(false == dbContext.OrderTimes.Any(x => x.ID == previousOrderTimeId), "OrderTime is still present"); } }
static void Main(string[] args) { using (var ctx = new EfTestDbContext()) { ctx.Database.EnsureCreated(); } Author existingAuthor; using (var ctx = new EfTestDbContext()) { Console.WriteLine($"Setup:"); var author = new Author { Name = "An author" }; Console.WriteLine($" Adding author ({author.Name}) to DB. Author's `IsKeySet`: {ctx.Entry(author).IsKeySet}"); ctx.Add(author); ctx.SaveChanges(); Console.WriteLine($" Saved author ({author.Name})"); Console.WriteLine($" Retrieving author from DB"); existingAuthor = ctx.Authors.First(); } using (var ctx = new EfTestDbContext()) { Console.WriteLine($"Adding new book and new author:"); var book = new Book() { Name = "First book", Author = new Author { Name = "Another author" } }; Console.WriteLine($" Adding a new book with a new author to DB. Book's `IsKeySet`: {ctx.Entry(book).IsKeySet}"); ctx.Add(book); ctx.SaveChanges(); Console.WriteLine($" Saved"); } Console.WriteLine($"Database contents:"); using (var ctx = new EfTestDbContext()) { Console.WriteLine($"Authors:"); foreach (var a in ctx.Authors) { Console.WriteLine($" {a.ID} - {a.Name}"); } Console.WriteLine($"Books & authors:"); foreach (var b in ctx.Books.Include(x => x.Author)) { Console.WriteLine($" {b.ID} - {b.Name} written by {b.Author.Name}"); } Console.WriteLine($""); } using (var ctx = new EfTestDbContext()) { Console.WriteLine($"Changing author of the book"); var bookToChange = ctx.Books.First(); bookToChange.Author = existingAuthor; Console.WriteLine($" Changed book: {bookToChange.ID} - {bookToChange.Name} written by {bookToChange.Author.Name}"); Console.WriteLine($" Existing author's `IsKeySet`: {ctx.Entry(existingAuthor).IsKeySet}"); ctx.SaveChanges(); Console.WriteLine($" Saved changes"); } using (var ctx = new EfTestDbContext()) { Console.WriteLine($"Adding a new book with an existing author"); var book = new Book() { Name = "First book", Author = existingAuthor }; Console.WriteLine($" Added author ({existingAuthor.Name}) to a new book. Author's `IsKeySet`: {ctx.Entry(existingAuthor).IsKeySet}"); ctx.Add(book); ctx.SaveChanges(); } Console.WriteLine("ALL DONE"); }