コード例 #1
0
 static void AddCustomer(Customer c)
 {
     using (ModelOneToManyContainerContainer context = new ModelOneToManyContainerContainer())
     {
         context.Customers.Add(c);
         context.SaveChanges();
     }
 }
コード例 #2
0
 static void PlaceOrder(Order o)
 {
     using (ModelOneToManyContainerContainer context = new ModelOneToManyContainerContainer())
     {
         context.Orders.Add(o);
         context.SaveChanges();
     }
 }
コード例 #3
0
        static void TestOneToMany()
        {
            Console.WriteLine("One to many association");
            using (ModelOneToManyContainerContainer context = new ModelOneToManyContainerContainer())
            {
                Customer c = new Customer()
                {
                    Name = "Customer 1",
                    City = "Iasi"
                };
                Order o1 = new Order()
                {
                    TotalValue = 200,
                    Date       = DateTime.Now,
                    Customer   = c
                };
                Order o2 = new Order()
                {
                    TotalValue = 300,
                    Date       = DateTime.Now,
                    Customer   = c
                };
                context.Customers.Add(c);
                context.Orders.Add(o1);
                context.Orders.Add(o2);
                context.SaveChanges();

                var items = context.Customers;
                foreach (var x in items)
                {
                    Console.WriteLine("Customer: {0}, {1}, {2}", x.CustomerId, x.Name, x.City);
                    foreach (var ox in x.Orders)
                    {
                        Console.WriteLine("\tOrders: {0}, {1}, {2}", ox.OrderId, ox.Date, ox.TotalValue);
                    }
                }
            }
        }