コード例 #1
0
        static void SetupDb(DbContextOptions <TestContext> dbOptions)
        {
            var context = new TestContext(dbOptions);

            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            // populate DB
            var entity1 = new Level1
            {
                Children =
                {
                    new Level2
                    {
                        Name   = "Foo",
                        Radius = 10,
                    },
                    new Level2
                    {
                        Name   = "Bar",
                        Radius = 20,
                    },
                },
            };

            context.Add(entity1);
            context.SaveChanges();
            Console.WriteLine($"{entity1.Children.Count} children initially - expecting 2.");
        }
コード例 #2
0
        static void Main(string[] args)
        {
            var options = new DbContextOptionsBuilder <TestContext>()
                          .EnableSensitiveDataLogging()
                          .UseSqlite("Data Source=database.db")
                          .Options;

            var context = new TestContext(options);

            context.Database.EnsureCreated();

            // populate DB
            var entity1 = new Level1
            {
                Children =
                {
                    new Level2
                    {
                        Children =
                        {
                            new Level3 {
                                Id = 1, Value = "1"
                            },
                            new Level3 {
                                Id = 2, Value = "2"
                            },
                        },
                    },
                },
            };
            var entity2 = new Level1
            {
                Children =
                {
                    new Level2
                    {
                        Children =
                        {
                            new Level3 {
                                Id = 3, Value = "3"
                            },
                            new Level3 {
                                Id = 4, Value = "4"
                            },
                        },
                    },
                },
            };

            context.Add(entity1);
            context.Add(entity2);
            context.SaveChanges();
            var lvl1Id = entity1.Id;
            var lvl2Id = entity1.Children.First().Id;

            // modify entity1
            context = new TestContext(options);
            var lvl2 = context.Level2
                       .Include(l2 => l2.Children)
                       .Single(l2 => l2.Id == lvl2Id);

            Console.WriteLine($"{lvl2.Children.Count} children (initially).");

            lvl2.Children.First().Value = "changed";
            lvl2.Children.Add(new Level3 {
                Value = "5"
            });

            // load other parents
            // XXX: this seems to confuse change tracking in EF Core to
            // ignore the previously added Level3 instance
            entity1 = context.Level1
                      .Include(l1 => l1.Children)
                      .ThenInclude(l2 => l2.Children)
                      .Single(l1 => l1.Id == lvl1Id);
            Console.WriteLine($"{lvl2.Children.Count} children (after modify, before save).");

            // this should update the first parent's children with an addtional entry
            // and one modification to an existing entry
            context.SaveChanges();

            // check what was inserted
            context = new TestContext(options);
            lvl2    = context.Level2
                      .Include(l2 => l2.Children)
                      .Single(l2 => l2.Id == lvl2Id);
            Console.WriteLine($"{lvl2.Children.Count} children (after save). Should be 3!");
            Console.ReadKey();
        }