Exemplo n.º 1
0
        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");
        }
Exemplo n.º 2
0
        public static async void ReplaceOrderTimeOnInternalOrderTrackedEntities()
        {
            // 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 order = dbContext.InternalOrders.Include(x => x.OrderTime)
                            .Single(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}");

                dbContext.SaveChanges();
                Console.WriteLine($"Current order time id after save {order.OrderTime}");
            }

            // 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");
                Debug.Assert(null != order.OrderTime);
                Debug.Assert("Test 1234" == order.OrderTime.Display);
            }
        }
Exemplo n.º 3
0
        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");
        }