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 }
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); } } }
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(); }
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(); }
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(); } }
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); } }
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(); } }
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); } }
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(); }
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; } }
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(); }
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(); } } }
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; } }
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(); }
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(); } }
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); } } }
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(); }
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(); }
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(); }
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); } } }
public ProductRepository(CustomerOrderContext context) : base(context) { }
public DeleteModel(CustomerOrderContext context) { _context = context; }
public CartDetailsRepository(CustomerOrderContext contextDb) : base(contextDb) { }
public ProductsController(CustomerOrderContext context) { _context = context; }
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"); } }
public CreateModel(CustomerOrderContext context) { _context = context; }
public EFReadOnlyRepository(CustomerOrderContext context) { _customerOrderContext = context; }
public IndexModel(CustomerOrderContext context) { _context = context; }
public CategoryRepository(CustomerOrderContext contextDb) : base(contextDb) { }