Example #1
0
 static void TestOneToMany()
 {
     Console.WriteLine("One to many association");
     using (ModelOneToManyContainer context = new ModelOneToManyContainer())
     {
         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);
             }
         }
     }
 }
Example #2
0
        static void OneToManyFromKeyboard()
        {
            using (ModelOneToManyContainer context = new ModelOneToManyContainer())
            {
                while (true)
                {
                    Console.WriteLine("--What would you like to enter into the database? 1 for customer, 2 for order--");
                    string choice = Console.ReadLine();

                    if (choice == "1")
                    {
                        Customer c = CustomerFromKeyboard();
                        context.Customers.Add(c);
                        context.SaveChanges();
                        var customers = context.Customers;
                        foreach (Customer x in customers)
                        {
                            Console.WriteLine("Customer : {0}, {1}, {2}", x.CustomerId, x.Name, x.City);
                        }
                    }
                    else if (choice == "2")
                    {
                        Console.WriteLine("--Do you have a customer for your order? y/n--");
                        string choice2 = Console.ReadLine();
                        if (choice2 == "y")
                        {
                            Console.WriteLine("--Enter the customer's Id--");
                            Customer customer   = null;
                            int      customerId = Convert.ToInt32(Console.ReadLine());

                            var customers = context.Customers;
                            foreach (Customer c in customers)
                            {
                                if (c.CustomerId == customerId)
                                {
                                    customer = c;
                                }
                            }
                            if (customer == null)
                            {
                                Console.WriteLine("--Customer not found--");
                                return;
                            }

                            Order o = OrderFromKeyboard(customer);
                            context.Orders.Add(o);
                            context.SaveChanges();
                        }
                        else if (choice2 == "n")
                        {
                            Customer c = CustomerFromKeyboard();
                            Order    o = OrderFromKeyboard(c);

                            context.Customers.Add(c);
                            context.Orders.Add(o);
                            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);
                        }
                    }
                }
            }
        }