public static void DeleteCustomer(string customerId) { using (var db = new NorthwindEntities()) { var customerToRemove = db.Customers.Where(c => c.CustomerID == customerId).First(); db.Customers.Remove(customerToRemove); db.SaveChanges(); } Console.WriteLine("Customer deleted!"); }
//2.Create a DAO class with static methods which provide functionality for inserting, modifying and deleting customers. public static void InsertCustomer( string customerID, string companyName, string contactName = null, string contactTitle = null, string address = null, string city = null, string region = null, string postalCode = null, string country = null, string phone = null, string fax = null) { using (var db = new NorthwindEntities()) { db.Customers.Add(new Customer() { CustomerID = customerID, CompanyName = companyName, ContactName = contactName, ContactTitle = contactTitle, Address = address, City = city, Region = region, PostalCode = postalCode, Country = country, Phone = phone, Fax = fax }); db.SaveChanges(); } Console.WriteLine("Inserted new customer!"); }
public static void UpdateCustomerPhone( string customerId, string phone) { using (var db = new NorthwindEntities()) { var customerToUpdate = db.Customers.Where(c => c.CustomerID == customerId).First(); customerToUpdate.Phone = phone; db.SaveChanges(); } Console.WriteLine("Customer phone changed!"); }
//7.Try to open two different data contexts and perform concurrent changes on the same records. public static void MakeConcurrentChanges() { using (var db = new NorthwindEntities()) { using (var anotherDB = new NorthwindEntities()) { var category = db.Categories.Where(c => c.CategoryName == "Beverages").First(); category.Description = "First change"; var category2 = anotherDB.Categories.Where(c => c.CategoryName == "Beverages").First(); category2.Description = "Second Change"; db.SaveChanges(); anotherDB.SaveChanges(); } } }