private static void AddCustomerandOrders() { var cust = new Customer { FirstName = "Paul", LastName = "Oakenfeld", Email = "*****@*****.**", Phone = "613 876-5555", Fax = "867-0987" }; var order1 = new Order { ProductName = "Sunglasses" }; var order2 = new Order { ProductName = "Shoes" }; using (var context = new CustomerContext()) { context.Database.Log = Console.WriteLine; context.Customers.Add(cust); cust.CustomerOrders.Add(order1); cust.CustomerOrders.Add(order2); context.SaveChanges(); } }
public void SaveNewOrder(Order order, int custId) { using (var context = new CustomerContext()) { var cust = context.Customers.Find(custId); cust.CustomerOrders.Add(order); context.SaveChanges(); } }
public void SaveUpdatedOrder(Order order, int custId) { using (var context = new CustomerContext()) { var orderFromDb = context.Orders.Include(n => n.Customer).FirstOrDefault(o => o.Id == order.Id); context.Entry(orderFromDb).CurrentValues.SetValues(order); context.SaveChanges(); } }