static void TestOneToMany() { Console.WriteLine("One to many association"); using (ModelOneToManyContainer context = new ModelOneToManyContainer()) { Console.WriteLine("Name:"); Console.WriteLine("City:"); Customer c = new Customer() { Name = Console.ReadLine(), City = Console.ReadLine() }; Console.WriteLine("TotalValue:"); Order o1 = new Order() { TotalValue = Int32.Parse(Console.ReadLine()), Date = DateTime.Now, Customer = c }; Console.WriteLine("TotalValue:"); Order o2 = new Order() { TotalValue = Int32.Parse(Console.ReadLine()), 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.Id, x.Name, x.City); foreach (var ox in x.Orders) { Console.WriteLine("\tOrders: {0}, {1}, {2}", ox.Id, ox.Date, ox.TotalValue); } } } }
static void TesTOneToMany() { Console.WriteLine("One to many association"); Console.Write("Enter customer name: "); string name = Console.ReadLine(); Console.Write("Enter customer city: "); string city = Console.ReadLine(); using (ModelOneToManyContainer context = new ModelOneToManyContainer()) { Customer c = new Customer() { Name = name, City = city }; 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); } } } }
static void TesTOneToMany() { Console.WriteLine("One to many association"); using (ModelOneToManyContainer context = new ModelOneToManyContainer()) { Customer c = new Customer(); Console.WriteLine("Give the Customer Name"); c.Name = Console.ReadLine(); Console.WriteLine("Give the Customer City"); c.City = Console.ReadLine(); Console.WriteLine("Give the number of orders"); context.Customers.Add(c); int nrOfOrders = Int32.Parse(Console.ReadLine()); for (int i = 1; i <= nrOfOrders; i++) { Order o = new Order(); Console.WriteLine("Give the Total Value of order"); o.TotalValue = Decimal.Parse(Console.ReadLine()); o.Date = DateTime.Now; o.Customer = c; context.Orders.Add(o); } context.SaveChanges(); var items = context.Customers.Include("Orders"); 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); } } } }
static void TestOneToMany() { Console.WriteLine("One to many association"); Console.WriteLine("Customer Name:"); string custName = Console.ReadLine(); Console.WriteLine("Customer City:"); string custCity = Console.ReadLine(); Console.WriteLine("How many orders?"); int noOfOrders = int.Parse(Console.ReadLine()); List <OrderType> orders = new List <OrderType>(); OrderType order; Console.WriteLine(""); for (int i = 0; i < noOfOrders; i++) { Console.WriteLine("Order {0}", i + 1); Console.WriteLine("Total Value:"); order.TotalValue = int.Parse(Console.ReadLine()); Console.WriteLine("Date (dd/MM/yyyy):"); order.Date = DateTime.ParseExact(Console.ReadLine(), "dd/MM/yyyy", CultureInfo.InvariantCulture); Console.WriteLine(""); orders.Add(order); } using (ModelOneToManyContainer context = new ModelOneToManyContainer()) { Customer c = new Customer() { Name = custName, City = custCity }; context.Customers.Add(c); for (int i = 0; i < noOfOrders; i++) { Order or = new Order() { TotalValue = orders[i].TotalValue, Date = orders[i].Date, Customer = c }; context.Orders.Add(or); } 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); } } } }