public static void DeleteCustomer(string customerId) { NorthwindEntities context = new NorthwindEntities(); Customer customer = context.Customers.Where(x => x.CustomerID == customerId).FirstOrDefault(); context.Customers.Remove(customer); context.SaveChanges(); }
public static string InsertCustomer(string customerId, string companyName) { NorthwindEntities context = new NorthwindEntities(); Customer customer = new Customer { CustomerID = customerId, CompanyName = companyName }; context.Customers.Add(customer); context.SaveChanges(); return customerId; }
public static void UpdateRegion() { NorthwindEntities context1 = new NorthwindEntities(); Region region1 = context1.Regions.First(); region1.RegionDescription = "Africa"; NorthwindEntities context2 = new NorthwindEntities(); Region region2 = context2.Regions.First(); region2.RegionDescription = "Europe"; context2.SaveChanges(); context1.SaveChanges(); // use transactions }
static void InsertDuplicateOrders() { NorthwindEntities context = new NorthwindEntities(); Order order = new Order { OrderID = 1, CustomerID = "QWE" }; Order order2 = new Order { OrderID = 1, CustomerID = "QWE" }; context.Orders.Add(order); context.Orders.Add(order2); context.SaveChanges(); }
static void InsertDifferentOrders() { NorthwindEntities context = new NorthwindEntities(); Order order1 = new Order { OrderID = 2, CustomerID = "QWE", }; Order order2 = new Order { OrderID = 3, CustomerID = "QWE" }; context.Orders.Add(order1); context.Orders.Add(order2); context.SaveChanges(); }
public static void UpdateCustomer(string customerId, Customer newCustomer) { NorthwindEntities context = new NorthwindEntities(); Customer customer = context.Customers.Where(x => x.CustomerID == customerId).FirstOrDefault(); if (customer == null) { throw new ArgumentNullException("The customer you are looking for does not exist!"); } customer.Address = newCustomer.Address; customer.City = newCustomer.City; customer.CompanyName = newCustomer.CompanyName; customer.ContactName = newCustomer.ContactName; customer.ContactTitle = newCustomer.ContactTitle; customer.Country = newCustomer.Country; customer.Fax = newCustomer.Fax; customer.Phone = newCustomer.Phone; customer.PostalCode = newCustomer.PostalCode; customer.Region = newCustomer.Region; context.SaveChanges(); }