예제 #1
0
 public static void Query1(CustomerOrderContext db)
 {
     // Database indexes are also useful. Table records are stored in the order of their clustered index, which by default is created
     // on the primary key, by can be set to another column if needed. ie date.
     // Indexes provide a quick lookup of information by a key value, which avoids the need for a full table scan (table scan is bad)
     // Indexes can also
 }
예제 #2
0
        public static void Query1(CustomerOrderContext db)
        {
            // Queries database
            // By querying to anonymous types, you essentially define a custom query that will only return the fields you
            // ask for. This is advantageous in situations where where the normal model includes a lot of unnecessary data

            var orders = db.Orders
                         .Select(order => new {
                order.Id,
                OrderItems = order.OrderItems.Select(orderItem =>
                                                     new {
                    orderItem.Quantity,
                    orderItem.Product.Description
                })
            }).ToList();

            foreach (var order in orders)
            {
                Console.WriteLine("Order number: " + order);

                foreach (var orderItem in order.OrderItems)
                {
                    var quantity    = orderItem.Quantity;
                    var description = orderItem.Description;

                    Console.Write($"Product: {description}", quantity);
                }
            }
        }
예제 #3
0
        public void OnGet([FromServices] CustomerOrderContext Db)
        {
            Db.Products.Add(new Product
            {
                name = "mk100",
            });
            Db.Products.Add(new Product
            {
                name = "mk200",
            });
            Db.Products.Add(new Product
            {
                name = "mk300",
            });
            Db.Products.Add(new Product
            {
                name = null,
            });
            Db.Products.Add(new Product
            {
                name = null,
            });

            Db.SaveChanges();
        }
예제 #4
0
        public static void Query1(CustomerOrderContext db)
        {
            // Queries database
            var orders = db.Orders
                         // Explicitly include other tables. EF then queries all information as a single joined query
                         .Include(x => x.OrderItems)
                         .Include(x => x.OrderItems.Select(oi => oi.Product))
                         // Adding `AsNoTracking` means that EF doesn't do any change tracking on the entities. Which makes querying
                         // and manipulation of the returned results faster
                         .AsNoTracking()
                         .ToList();

            foreach (var order in orders)
            {
                Console.WriteLine("Order number: " + order);

                // And queries database again for each order to get the order items
                foreach (var orderItem in order.OrderItems)
                {
                    var quantity = orderItem.Quantity;
                    // And again for each product on each order item
                    var description = orderItem.Product.Description;

                    Console.Write($"Product: {description}", quantity);
                }
            }
        }
 public void OnGet([FromServices] CustomerOrderContext Db)
 {
     //Db.Products.Add(new Product
     //{
     //    name = "mk100",
     //    category = "Man",
     //    price = 100
     //});
     //Db.Products.Add(new Product
     //{
     //    name = "mk200",
     //    category = "Man",
     //    price = 120
     //});
     //Db.Products.Add(new Product
     //{
     //    name = "mk300",
     //    category = "Women",
     //    price = 140
     //});
     //Db.Products.Add(new Product
     //{
     //    name = "mk400",
     //    category = "Women",
     //    price = 130
     //});
     //Db.SaveChanges();
 }
예제 #6
0
        public CustomersController(CustomerOrderContext context)
        {
            _context = context;

            if (_context.Customers.Count() == 0)
            {
                IList <Customer> customers = new List <Customer>
                {
                    new Customer
                    {
                        Name        = "Jonier",
                        HomeAddress = new Address {
                            City = "Redmond", Street = "156 AVE NE"
                        },
                        Order = new Order {
                            Title = "104m"
                        },
                        Orders = Enumerable.Range(0, 2).Select(e => new Order {
                            Title = "abc" + e
                        }).ToList()
                    },
                    new Customer
                    {
                        Name        = "Sam",
                        HomeAddress = new Address {
                            City = "Bellevue", Street = "Main St NE"
                        },
                        Order = new Order {
                            Title = "Zhang"
                        },
                        Orders = Enumerable.Range(0, 2).Select(e => new Order {
                            Title = "xyz" + e
                        }).ToList()
                    },
                    new Customer
                    {
                        Name        = "Peter",
                        HomeAddress = new Address {
                            City = "Hollewye", Street = "Main St NE"
                        },
                        Order = new Order {
                            Title = "Jichan"
                        },
                        Orders = Enumerable.Range(0, 2).Select(e => new Order {
                            Title = "ijk" + e
                        }).ToList()
                    },
                };

                foreach (var customer in customers)
                {
                    _context.Customers.Add(customer);
                    _context.Orders.Add(customer.Order);
                    _context.Orders.AddRange(customer.Orders);
                }

                _context.SaveChanges();
            }
        }
예제 #7
0
        public static void Delete1(CustomerOrderContext db)
        {
            var customers = db.Customers.OrderByDescending(x => x.Id).Take(100).ToList();

            foreach (var customer in customers)
            {
                db.Customers.Remove(customer);
            }
        }
예제 #8
0
        public CustomersController(CustomerOrderContext context)
        {
            _context = context;

            if (_context.Customers.Count() == 0)
            {
                IList <Customer> customers = new List <Customer>
                {
                    new Customer
                    {
                        Name        = "Jonier",
                        ByteValue   = 8,
                        Data        = new byte[] { 1, 2, 3 },
                        HomeAddress = new Address {
                            City = "Redmond", Street = "156 AVE NE"
                        },
                        Order = new Order {
                            Title = "104m"
                        }
                    },
                    new Customer
                    {
                        Name        = "Sam",
                        ByteValue   = 18,
                        Data        = new byte[] { 4, 5, 6 },
                        HomeAddress = new Address {
                            City = "Bellevue", Street = "Main St NE"
                        },
                        Order = new Order {
                            Title = "Zhang"
                        }
                    },
                    new Customer
                    {
                        Name        = "Peter",
                        ByteValue   = 28,
                        Data        = new byte[] { 7, 8, 9 },
                        HomeAddress = new Address {
                            City = "Hollewye", Street = "Main St NE"
                        },
                        Order = new Order {
                            Title = "Jichan"
                        }
                    },
                };

                foreach (var customer in customers)
                {
                    _context.Customers.Add(customer);
                    _context.Orders.Add(customer.Order);
                }

                _context.SaveChanges();
            }
        }
예제 #9
0
 static void Main(string[] args)
 {
     using (var db = new CustomerOrderContext())
     {
         //SimpleQueries.Query1(db);
         //SimpleQueries.AddData1(db);
         //NoTrackingQueries.Query1(db);
         //NoTrackingQueries.AddData1(db);
         //IncludedQueries.Query1(db);
         //IncludedQueries.AddData1(db);
         //AnonymousQueries.Query1(db);
     }
 }
예제 #10
0
        public static void AddData1(CustomerOrderContext db)
        {
            for (int i = 0; i < 100; i++)
            {
                // EF detects each of these as a discrete change, and processes in the insert statements one at a time
                db.Customers.Add(new Customer
                {
                    Name    = $"Customer {i}",
                    Balance = i
                });
            }

            db.SaveChanges();
        }
예제 #11
0
        public static void Delete1(CustomerOrderContext db)
        {
            var customers = db.Customers
                            .AsNoTracking()
                            .OrderByDescending(x => x.Id)
                            .Take(100)
                            .ToList();

            foreach (var customer in customers)
            {
                db.Customers.Attach(customer);
                db.Entry(customer).State = EntityState.Deleted;
            }
        }
예제 #12
0
        static void Main(string[] args)
        {
            Customer c = new Customer {
                Name = "Claudiu", City = "Iasi"
            };
            Order o = new Order {
                customer = c, TotalValue = 200, Date = DateTime.Now
            };
            CustomerOrderContext context = new CustomerOrderContext();

            context.Customers.Add(c);
            context.Orders.Add(o);
            context.SaveChanges();
        }
예제 #13
0
        public CustomersController(CustomerOrderContext context)
        {
            _db = context;

            if (context.Database.EnsureCreated())
            {
                if (context.Customers.Count() == 0)
                {
                    foreach (var customer in DataSource.GetCustomers())
                    {
                        context.Customers.Add(customer);
                        context.Orders.Add(customer.Order);
                    }

                    context.SaveChanges();
                }
            }
        }
예제 #14
0
        public static void Delete1(CustomerOrderContext db)
        {
            var customerIds = db.Customers
                              .OrderByDescending(x => x.Id)
                              .Select(x => x.Id)
                              .Take(100)
                              .ToList();

            foreach (var id in customerIds)
            {
                var customer = new Customer()
                {
                    Id = id
                };
                db.Customers.Attach(customer);
                db.Entry(customer).State = EntityState.Deleted;
            }
        }
예제 #15
0
        public static void AddData1(CustomerOrderContext db)
        {
            var customers = new List <Customer>();

            for (int i = 0; i < 100; i++)
            {
                // EF detects each of these as a discrete change, and processes in the insert statements one at a time
                customers.Add(new Customer
                {
                    Name    = $"Customer {i}",
                    Balance = i
                });
            }

            // When you use AddRange, all the inserts are done as part of the one operation, dramatically reducing the overhead
            db.Customers.AddRange(customers);
            db.SaveChanges();
        }
예제 #16
0
        public HandleCustomerController(CustomerOrderContext context)
        {
            _db = context;
            _db.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
            if (context.Customers.Count() == 0)
            {
                foreach (var b in DataSource.GetCustomers())
                {
                    context.Customers.Add(b);
                    foreach (var order in b.Orders)
                    {
                        context.Orders.Add(order);
                    }
                }

                context.SaveChanges();
            }
        }
예제 #17
0
        public static void Query1(CustomerOrderContext db)
        {
            // Queries database
            var orders = db.Orders.ToList();

            foreach (var order in orders)
            {
                Console.WriteLine("Order number: " + order);

                // And queries database again for each order to get the order items
                foreach (var orderItem in order.OrderItems)
                {
                    var quantity = orderItem.Quantity;
                    // And again for each product on each order item
                    var description = orderItem.Product.Description;

                    Console.Write($"Product: {description}", quantity);
                }
            }
        }
예제 #18
0
        public static void AddData1(CustomerOrderContext db)
        {
            for (int id = 0; id < 100; id++)
            {
                var balance = id;
                // EF detects each of these as a discrete change, and processes in the insert statements one at a time
                var customer = new Customer
                {
                    Name    = $"Customer Name",
                    Balance = balance
                };

                db.Customers.Attach(customer);
                db.Entry(customer).State = EntityState.Added;
                db.Entry(customer).State = EntityState.Modified;
                db.Entry(customer).State = EntityState.Deleted;
            }

            db.SaveChanges();
        }
예제 #19
0
        public static void Generate(CustomerOrderContext context)
        {
            if (context.Customers.Any())
            {
                return;
            }

            var customers = GetCustomers();

            foreach (var c in customers)
            {
                foreach (var o in c.Orders)
                {
                    context.Orders.Add(o);
                }

                context.Customers.Add(c);
            }
            context.SaveChanges();
        }
예제 #20
0
 public void OnGet([FromServices] CustomerOrderContext Db)
 {
     Db.Products.Add(new Product
     {
         name   = "mk100",
         image  = null,
         images = new List <Image> {
             new Image {
                 src = "da", size = 5
             }, new Image {
                 src = "da", size = 5
             }
         }
     });
     Db.Products.Add(new Product
     {
         name  = "mk200",
         image = new Image {
             src = "aa", size = 6
         }
     });
     Db.SaveChanges();
 }
예제 #21
0
        public static void Query1(CustomerOrderContext db)
        {
            // Queries database
            var orders = db.Orders
                         // Explicitly include other tables. EF then queries all information as a single joined query
                         .Include(x => x.OrderItems)
                         .Include(x => x.OrderItems.Select(oi => oi.Product))
                         .ToList();

            foreach (var order in orders)
            {
                Console.WriteLine("Order number: " + order.Id);

                // And queries database again for each order to get the order items
                foreach (var orderItem in order.OrderItems)
                {
                    var quantity = orderItem.Quantity;
                    // And again for each product on each order item
                    var description = orderItem.Product.Description;

                    Console.WriteLine($"Product: {description}", quantity);
                }
            }
        }
예제 #22
0
 public ProductRepository(CustomerOrderContext context) : base(context)
 {
 }
예제 #23
0
 public DeleteModel(CustomerOrderContext context)
 {
     _context = context;
 }
예제 #24
0
 public CartDetailsRepository(CustomerOrderContext contextDb) : base(contextDb)
 {
 }
 public ProductsController(CustomerOrderContext context)
 {
     _context = context;
 }
예제 #26
0
        static void Main(string[] args)
        {
            var customerNames = File.ReadAllLines("CustomerNames.txt");
            var productNames  = File.ReadAllLines("ProductNames.txt");

            using (var db = new CustomerOrderContext())
            {
                var random = new Random();

                foreach (var customerName in customerNames.Select(x => x.Trim()).Where(x => !string.IsNullOrWhiteSpace(x)))
                {
                    Console.WriteLine("Creating customer: " + customerName);
                    db.Customers.Add(new Customer()
                    {
                        Name    = customerName,
                        Balance = (decimal)(Math.Round(random.NextDouble() * 1000, 2))
                    });
                }

                foreach (var productName in productNames.Select(x => x.Trim()).Where(x => !string.IsNullOrWhiteSpace(x)))
                {
                    var uncessesaryData = new Byte[10000];
                    random.NextBytes(uncessesaryData);

                    Console.WriteLine("Creating product: " + productName);
                    db.Products.Add(new Product
                    {
                        Description   = productName,
                        ImageLocation = productName + ".png",
                        Price         = (decimal)(Math.Round(random.NextDouble() * 333, 2)),
                        // Need to populate the large data field
                        LargeUncessesaryDataField = uncessesaryData
                    });
                }

                db.SaveChanges();

                var customers     = db.Customers.Select(x => x.Id).ToArray();
                var products      = db.Products.Select(x => x.Id).ToArray();
                var productPrices = db.Products.ToDictionary(x => x.Id, x => x.Price);

                var orders = new List <Order>();

                for (var orderNo = 0; orderNo < 10000; orderNo++)
                {
                    var customerId = customers[random.Next(customers.Length)];

                    var order = new Order {
                        CustomerId = customerId
                    };
                    var orderLines = random.Next(1, 10);

                    Console.WriteLine($"Creating order {orderNo} for customer {customerId} with {orderLines} items");

                    for (var lineNo = 0; lineNo < orderLines; lineNo++)
                    {
                        var productId = products[random.Next(products.Length)];
                        var quantity  = random.Next(100);
                        order.OrderItems.Add(new OrderItem
                        {
                            Quantity  = quantity,
                            ProductId = productId,
                            ItemPrice = productPrices[productId],
                            LinePrice = productPrices[productId] * quantity
                        });
                    }

                    order.OrderTotal = order.OrderItems.Sum(x => x.ItemPrice);
                    var dayOfYear = random.Next(0, 364);
                    order.OrderDate = new DateTime(2019, 1, 1).AddDays(dayOfYear);

                    orders.Add(order);
                }

                Console.WriteLine("Saving orders");
                db.Orders.AddRange(orders);

                db.SaveChanges();
                Console.WriteLine("Done");
            }
        }
예제 #27
0
 public CreateModel(CustomerOrderContext context)
 {
     _context = context;
 }
예제 #28
0
 public EFReadOnlyRepository(CustomerOrderContext context)
 {
     _customerOrderContext = context;
 }
예제 #29
0
 public IndexModel(CustomerOrderContext context)
 {
     _context = context;
 }
예제 #30
0
 public CategoryRepository(CustomerOrderContext contextDb) : base(contextDb)
 {
 }