static void Main(string[] args)
        {
            Console.WriteLine($"Setup...");
            using (var context = new MyDbContext(useLazyLoading: false))
            {
                context.Database.EnsureDeleted();
                context.Database.EnsureCreated();

                var parent = new Parent
                {
                    Child = new ConcreteChild
                    {
                        GrandChildren = new List <GrandChild>
                        {
                            new GrandChild()
                        }
                    }
                };
                context.Parents.Add(parent);
                context.SaveChanges();
            }

            Console.WriteLine($"Eagerly loading GrandChild entities fails (lazy loading ENABLED, no tracking)");

            try
            {
                using (var context = new MyDbContext(useLazyLoading: true))
                {
                    var results = context.Parents
                                  .AsNoTracking()
                                  .Include(x => x.Child.GrandChildren)
                                  .ToList();

                    var child = results.Single().Child;

                    // Exception is thrown when accessing child.GrandChildren
                    Console.WriteLine($"GrandChildren.Count: {child.GrandChildren.Count}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception: {ex.Message}");
            }


            Console.WriteLine();
            Console.WriteLine("----------------------------------------------");
            Console.WriteLine();


            Console.WriteLine($"Eagerly loading too many GrandChild entities (lazy loading DISABLED, no tracking)");

            using (var context = new MyDbContext(useLazyLoading: false))
            {
                var results = context.Parents
                              .AsNoTracking()
                              .Include(x => x.Child.GrandChildren)
                              .Select(parent => new
                {
                    Parent = parent,
                    Child  = parent.Child
                })
                              .ToList();

                var child = results.Single().Child;
                Console.WriteLine($"GrandChildren.Count expected: 1");
                Console.WriteLine($"GrandChildren.Count actual: {child.GrandChildren.Count}");
            }
        }
Example #2
0
        public Form1()
        {
            InitializeComponent();

            initializeDateTimePicker();

            dataGridView1.AllowUserToAddRows    = false;
            dataGridView1.AllowUserToDeleteRows = false;

            buttonAdd.Click    += buttonAdd_Click;
            buttonDelete.Click += buttonDelete_Click;
            buttonEdit.Click   += buttonEdit_Click;

            using (MyDbContext context = new MyDbContext())
            {
                context.Database.EnsureCreated();
            }

            using (MyDbContext context = new MyDbContext())
            {
                context.Database.EnsureCreated();

                MyData data = context.Find <MyData>(1u);
                if (null != data)
                {
                    Console.WriteLine("Found");
                    Console.WriteLine(data.Name);
                }
                else
                {
                    Console.WriteLine("Not Found");
                }
                BindingSource bi = new BindingSource();
                bi.DataSource            = context.Find <MyData>(1u);
                dataGridView1.DataSource = bi;
                dataGridView1.Refresh();
            }

            using (MyDbContext context = new MyDbContext())
            {
                context.Database.EnsureCreated();

                MyData found = null;
                foreach (var item in from data in context.Data
                         where data.Key == 1
                         select data)
                {
                    found = item;
                    break;
                }

                if (null == found)
                {
                    return;
                }
                //found.Number++;
                context.SaveChanges();
            }

            using (MyDbContext context = new MyDbContext())
            {
                context.Add(new MyData()
                {
                    Name = "이름", Number = 123
                });
                context.SaveChanges();
            }
        }
Example #3
0
 public void Create(StudentEntity student)
 {
     _ctx.Students.Add(student);
     _ctx.SaveChanges();
 }
Example #4
0
 public ProductEntity CreateProduct(ProductEntity product)
 {
     product = _ctx.Products.Add(product);
     _ctx.SaveChanges();
     return(product);
 }