public static void Delete(string id) { using (NORTHWNDEntities db = new NORTHWNDEntities()) { var customer = db.Customers.Where(x => x.CustomerID == id).FirstOrDefault(); db.Customers.Remove(customer); db.SaveChanges(); } }
public static void Modify(string id, string newContactName) { using (NORTHWNDEntities db = new NORTHWNDEntities()) { var customer = db.Customers.Where(x => x.CustomerID == id).FirstOrDefault(); customer.ContactName = newContactName; db.SaveChanges(); } }
public static void Insert(string id, string name) { Customer newCustomer = new Customer() { CompanyName = name, CustomerID = id }; using (NORTHWNDEntities db = new NORTHWNDEntities()) { bool isInDB = IsInDataBase(db, id); if (!isInDB) { db.Customers.Add(newCustomer); db.SaveChanges(); Console.WriteLine("Added Successful."); } else { throw new ArgumentException("Such customer already exists"); } } }
// Task 9 private static void CreateNewOrder(Order order) { using (TransactionScope scope = new TransactionScope()) { NORTHWNDEntities db = new NORTHWNDEntities(); var customer = db.Customers.First(c => c.CustomerID == order.CustomerID); if (customer != null) { order.ShipAddress = customer.Address; order.ShipCity = customer.City; order.ShipPostalCode = customer.PostalCode; order.ShipCountry = customer.Country; } db.Orders.Add(order); db.SaveChanges(); scope.Complete(); } }
// Task 7 private static void ConcurrentChages() { using (TransactionScope scope = new TransactionScope()) //using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new // TransactionOptions { IsolationLevel= IsolationLevel.Snapshot })) { NORTHWNDEntities db = new NORTHWNDEntities(); Product product = db.Products.First(p => p.ProductID == 1); product.ProductName = "Test 001"; db.SaveChanges(); NORTHWNDEntities db2 = new NORTHWNDEntities(); Product product2 = db2.Products.First(p => p.ProductID == 1); product2.ProductName = "Test 002"; try { int result = db2.SaveChanges(); Console.WriteLine(result.ToString() + " products changed"); } catch (OptimisticConcurrencyException) { db2.Entry(product2).Reload(); //db2.Refresh(RefreshMode.ClientWins, db2.Products); db2.SaveChanges(); } scope.Complete(); } }