Ejemplo n.º 1
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();
        }
Ejemplo n.º 2
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();
            }
        }
Ejemplo n.º 3
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();
            }
        }
Ejemplo n.º 4
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();
        }
Ejemplo n.º 5
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();
        }
Ejemplo n.º 6
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();
                }
            }
        }
Ejemplo n.º 7
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();
            }
        }
Ejemplo n.º 8
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();
        }
Ejemplo n.º 9
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();
        }
Ejemplo n.º 10
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();
        }
Ejemplo n.º 11
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();
 }
Ejemplo n.º 12
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");
            }
        }
Ejemplo n.º 13
0
 public int Complete()
 {
     return(_context.SaveChanges());
 }
Ejemplo n.º 14
0
 public IActionResult Post([FromBody] Customer customer)
 {
     _context.Customers.Add(customer);
     _context.SaveChanges();
     return(Created(customer));
 }