public List<Customer> GetCustomers() { using(var context = new CustomerContext ()) { return context.Customers.AsNoTracking().OrderBy(n => n.LastName).ToList(); } }
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 Customer GetCustomerById(int id) { using(var context = new CustomerContext ()) { return context.Customers.AsNoTracking().SingleOrDefault(n => n.Id == id); } }
private static void AddMultipleCustomers() { var cust1 = new Customer { FirstName = "Pete", LastName = "Townsend", Email = "*****@*****.**", Phone = "613 555-2355", Fax = "867-598" }; var cust2 = new Customer { FirstName = "John", LastName = "Entwistle", Email = "*****@*****.**", Phone = "613 444-5555", Fax = "345-5909" }; using (var context = new CustomerContext()) { context.Database.Log = Console.WriteLine; context.Customers.AddRange(new List<Customer> {cust1, cust2}); context.SaveChanges(); } }
public void SaveUpdatedCustomer(Customer cust) { using (var context = new CustomerContext()) { context.Entry(cust).State = EntityState.Modified; context.SaveChanges(); } }
public void SaveNewCustomer(Customer cust) { using(var context = new CustomerContext ()) { context.Customers.Add(cust); context.SaveChanges(); } }
public Customer GetCustomerOrders(int id) { using (var context = new CustomerContext()) { return context.Customers.AsNoTracking().Include(n => n.CustomerOrders).FirstOrDefault(n => n.Id == id); } }
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 DeleteCustomer(int id) { using (var context = new CustomerContext()) { var cust = context.Customers .Find (id); context.Entry(cust).State = EntityState.Deleted; 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(); } }
private static void AddCustomer() { var cust = new Customer { FirstName = "Patrick", LastName = "Clarke", Email = "*****@*****.**", Phone = "613 555-5555", Fax = "867-5309" }; using(var context = new CustomerContext()) { context.Database.Log = Console.WriteLine; context.Customers.Add(cust); context.SaveChanges(); } }
private static void UpdateCustomers() { using (var context = new CustomerContext()) { string fname = "P"; var customer = context.Customers.Where(c => c.FirstName.StartsWith(fname)).OrderBy(c => c.LastName).FirstOrDefault(); customer.Fax = "613 222-6543"; context.SaveChanges(); } }
private static void GetSomeCustomers() { using (var context = new CustomerContext()) { string fname = "P"; var customers = context.Customers.Where(c => c.FirstName.StartsWith(fname)).OrderBy(c => c.LastName); foreach (var cust in customers) { Console.WriteLine(cust.FirstName + " " + cust.LastName); } } }
private static void GetCustomers() { using (var context = new CustomerContext()) { var customers = context.Customers.ToList(); foreach(var cust in customers) { Console.WriteLine(cust.FirstName + " " + cust.LastName); } } }
private static void GetCustomerOrders() { using (var context = new CustomerContext()) { string lname = "Oakenfeld"; var cust = context.Customers.Include(c=>c.CustomerOrders) .Where(c => c.LastName == lname).FirstOrDefault(); Console.WriteLine(cust.FirstName + " " + cust.LastName); foreach(var o in cust.CustomerOrders) { Console.WriteLine(o.ProductName); } } }
private static void DeleteCustomer() { using (var context = new CustomerContext()) { var customer = context.Customers.FirstOrDefault(); context.Customers.Remove(customer); context.SaveChanges(); } }
private static void FindCustomer() { using (var context = new CustomerContext()) { var customer = context.Customers.Find(7); Console.WriteLine(customer.FirstName + " " + customer.LastName); } }