public static void DeleteCustomerRecored(string customerId) { using (var northwindEntities = new NorthwindEntities()) { var customer = CustomersRepository.GetCustomerById(northwindEntities, customerId); northwindEntities.Customers.Remove(customer); northwindEntities.SaveChanges(); } }
public static void AddCustomerRecord(string customerId, string companyName) { using (var northwindEntities = new NorthwindEntities()) { northwindEntities.Customers.Add(new Customer { CustomerID = customerId, CompanyName = companyName }); northwindEntities.SaveChanges(); } }
//7.Try to open two different data contexts and perform concurrent changes on the same records. public static void TwoContexts() { using (var dbOne = new NorthwindEntities()) { using (var dbTwo = new NorthwindEntities()) { var pesho = dbOne.Customers.FirstOrDefault(); var gosho = dbTwo.Customers.FirstOrDefault(); pesho.ContactName = "Pesho"; gosho.ContactName = "Gosho"; dbOne.SaveChanges(); dbTwo.SaveChanges(); } } }
// 7. // Try to open two different data contexts and perform concurrent changes on the same records. // What will happen at SaveChanges()? // How to deal with it? public static void DifferentContexts() { using (NorthwindEntities db1 = new NorthwindEntities(), db2 = new NorthwindEntities()) { var customer1 = db1.Customers.FirstOrDefault(); Console.WriteLine(customer1.ContactName); customer1.ContactName = "Ivan"; var customer2 = db2.Customers.FirstOrDefault(); Console.WriteLine(customer2.ContactName); customer2.ContactName = "Pesho"; db1.SaveChanges(); db2.SaveChanges(); } }
public static void UpdateCustomerRecord(string customerId, string contactName) { using (var northwindEntities = new NorthwindEntities()) { var customer = CustomersRepository.GetCustomerById(northwindEntities, customerId); customer.ContactName = contactName; northwindEntities.SaveChanges(); } }
//2.Create a DAO class with static methods which provide functionality for inserting, modifying and deleting customers. public static void InsertCustomer(Customer customer, NorthwindEntities db) { db.Customers.Add(customer); db.SaveChanges(); }