示例#1
0
        static void Main(string[] args)
        {
            using (var db = new SouthwindContext())
            {
                //Console.WriteLine("Creating two customers");
                //db.Add(new Customer { CustomerId = "MANDA", City = "Birmingham"
                //    , ContactName = "Nish Mandal", Country = "UK", PostalCode = "B74BB" });

                //db.Add(new Customer
                //{
                //    CustomerId = "FRENC",
                //    City = "Birmingham",
                //    ContactName = "Cathy French",
                //    Country = "UK",
                //    PostalCode = "ABC123"
                //});
                //db.SaveChanges();

                //Read a query
                //Console.WriteLine("Querying for a customer");

                //var customerQuery = db.Customers.OrderBy(c => c.ContactName);
                //var customer = customerQuery.Where(c => c.ContactName.Contains("Cath")).FirstOrDefault();
                //Console.WriteLine($"Customer details: {customer.ContactName} lives in {customer.City}");

                ////update a customer
                //var nish1 = db.Customers.Find("MANDA");
                //nish1.City = "London";
                //db.SaveChanges();

                ////find a customer and create an order for them
                //var nish2 = db.Customers.Find("MANDA");
                //nish1.Orders.Add(new Order { CustomerId = "MANDA", OrderDate = DateTime.Now, ShipCountry = "Brazil" });
                //db.SaveChanges();

                ////create and order and associate it with a customer
                //var nish3 = db.Customers.Find("MANDA");
                //var order1 = new Order { OrderDate = DateTime.Now, ShipCountry = "India", Customer = nish3 };
                //db.SaveChanges();

                //var order = db.Orders.Find(1);

                //var orderDetails1 = new OrderDetail { UnitPrice = 30.00m, Discount = 0.5f, Quantity = 2 };
                //var orderDetails2 = new OrderDetail { UnitPrice = 10.00m, Discount = 0.25f, Quantity = 1 };

                //var orderDetailsList = new List<OrderDetail>();
                //orderDetailsList.Add(orderDetails1);
                //orderDetailsList.Add(orderDetails2);

                //order.OrderDetails = orderDetailsList;
                //db.SaveChanges();

                ////Include the list of orders when a customer is retrieved
                ///
                //var custQuery = db.Customers.OrderBy(c => c.ContactName);

                //foreach (var c in custQuery.Include(c => c.Orders))
                //{
                //    Console.WriteLine($"{c.ContactName} live in {c.City}");
                //    if (c.Orders.Count >0)

                //    foreach (var orders in c.Orders)
                //        {
                //            Console.WriteLine($"\tOrder {orders.OrderId} by {orders.Customer.ContactName}" +
                //                $" was made on {orders.OrderDate.Value.Date}");
                //        }
                //}
                //var orderDetails = db.OrderDetails.Include(od => od.Order).ThenInclude(o => o.OrderDetails);
                //orderDetails.ToList().ForEach(od => Console.WriteLine($"{od.Order.Customer.ContactName} made an order" +
                //    $" with ID {od.Order.OrderId} with the unit price of {od.UnitPrice} without discount"));
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            using (var db = new SouthwindContext())
            {
                //db.Add(new Customer { City = "Bedford", ContactName = "Uzair", Country = "UK", CustomerId = "UZZEC", PostalCode = "MAD45AS" });
                //db.Add(new Customer { City = "Bedford", ContactName = "Uzair", Country = "UK", CustomerId = "UZZEG", PostalCode = "MAD45AS" });

                //db.SaveChanges();

                //db.Customers.Remove(db.Customers.Find("UZZEC"));
                //db.Customers.Remove(db.Customers.Find("UZZEG"));

                //db.SaveChanges();

                //Create order
                //var customer = db.Customers.Find("UZZEC");
                //customer.Orders.Add(new() { CustomerId = "UZZEC", OrderDate = DateTime.Now, ShipCountry = "UK" });
                //db.SaveChanges();

                //db.Add(new Order() { CustomerId = "UZZEC", Customer = customer, OrderDate = DateTime.Now, ShipCountry = "USA" });
                //db.SaveChanges();

                //db.Add(new Order() { CustomerId = "UZZEC", OrderDate = DateTime.Now, ShipCountry = "USA" });
                //db.SaveChanges();

                //db.Add(new Order() { OrderDate = DateTime.Now, ShipCountry = "USA" });
                //db.SaveChanges();

                //customer.Orders.ToList().ForEach(x => Console.WriteLine(x.ShipCountry));

                //customer =
                //    db.Customers
                //    .Include(x => x.Orders)
                //    .Where(x => x.CustomerId == "UZZEC")
                //    .FirstOrDefault();

                //customer.Orders.ToList().ForEach(x => Console.WriteLine(x.ShipCountry));


                //db.Orders.ToList().ForEach(x => Console.WriteLine(x.ShipCountry));
                //db.Orders.ToList().ForEach(x => Console.WriteLine(x.Customer.ContactName));

                //var order = db.Orders.Find(9);

                //var orderDetail1 = new OrderDetail() { Discount = 0.05f, Quantity = 2, UnitPrice = 6, ProductId = 7 };
                //var orderDetail2 = new OrderDetail() { Discount = 0.1f, Quantity = 1, UnitPrice = 8 ,ProductId = 8 };

                //var orderDetailList = new List<OrderDetail>();

                //orderDetailList.Add(orderDetail1);
                //orderDetailList.Add(orderDetail2);

                //order.OrderDetails = orderDetailList;
                //db.SaveChanges();

                //var custQuery = db.Customers.OrderBy(c => c.ContactName);

                //foreach (var item in custQuery.Include(c => c.Orders))
                //{
                //    Console.WriteLine($"{item.ContactName} lives in {item.City}");

                //    foreach (var o in item.Orders)
                //    {
                //        Console.WriteLine($"   {o.OrderId} was made on {o.OrderDate}");
                //    }
                //}

                //var orderDetails = db.OrderDetails.Include(c => c.Order).ThenInclude(c => c.Customer);

                //orderDetails.ToList().ForEach(x => Console.WriteLine($"Customer: {x.Order.Customer.ContactName}, Order: {x.Order.OrderId}, OrderPrice: {x.UnitPrice}"));

                var customers =
                    from Customer c in db.Customers
                    where c.CustomerId == "UZAIR"
                    select c;
            }
        }