static void TestTables() { using (NorthwindEntities NWEntities = new NorthwindEntities()) { // retrieve all Beverages products IEnumerable<Product> beverages = from p in NWEntities.Products where p.Category.CategoryName == "Beverages" orderby p.ProductName select p; Console.WriteLine("There are {0} Beverages", beverages.Count()); // update one product Product bev1 = beverages.ElementAtOrDefault(10); if (bev1 != null) { decimal newPrice = (decimal)bev1.UnitPrice + 10.00m; Console.WriteLine("The price of {0} is {1}. Update to {2}", bev1.ProductName, bev1.UnitPrice, newPrice); bev1.UnitPrice = newPrice; } // submit the change to database NWEntities.SaveChanges(); // insert a product Product newProduct = new Product { ProductName = "new test product" }; NWEntities.Products.Add(newProduct); NWEntities.SaveChanges(); Console.WriteLine("Added a new product"); // delete a product IQueryable<Product> productsToDelete = from p in NWEntities.Products where p.ProductName == "new test product" select p; if (productsToDelete.Count() > 0) { foreach (var p in productsToDelete) { NWEntities.Products.Remove(p); Console.WriteLine("Deleted product {0}", p.ProductID); } NWEntities.SaveChanges(); } } }
static void TestImplicitTransaction() { using (NorthwindEntities NWEntities = new NorthwindEntities()) { Product prod1 = (from p in NWEntities.Products where p.ProductID == 4 select p).First(); Product prod2 = (from p in NWEntities.Products where p.ProductID == 5 select p).First(); prod1.UnitPrice += 1; // update will be saved to database NWEntities.SaveChanges(); Console.WriteLine("First update saved to database"); prod2.UnitPrice = -5; // update will fail because UnitPrice can't be < 0 // but previous update stays in database try { NWEntities.SaveChanges(); Console.WriteLine("Second update saved to database"); } catch (Exception) { Console.WriteLine("Second update not saved to database"); } } }
static void TestExplicitTransaction() { using (NorthwindEntities NWEntities = new NorthwindEntities()) { using (TransactionScope ts = new TransactionScope()) { try { Product prod1 = (from p in NWEntities.Products where p.ProductID == 4 select p).First(); prod1.UnitPrice += 1; NWEntities.SaveChanges(); Console.WriteLine("First update saved to database, but not commited."); // now let's try to update another product Product prod2 = (from p in NWEntities.Products where p.ProductID == 5 select p).First(); // update will fail because UnitPrice can't be < 0 prod2.UnitPrice = -5; NWEntities.SaveChanges(); ts.Complete(); } catch (Exception e) { // both updates will fail because they are within one transaction Console.WriteLine("Exception caught. Rollback the first update."); } } } }
static void TestVersionControl() { using (NorthwindEntities NWEntities = new NorthwindEntities()) { // first user Console.WriteLine("First User ..."); Product product = (from p in NWEntities.Products where p.ProductID == 3 select p).First(); Console.WriteLine("Original unit in stock: {0}", product.UnitsInStock); product.UnitsInStock += 1; Console.WriteLine("Current unit in stock to update: {0}", product.UnitsInStock); // process more products // second user Console.WriteLine("\nSecond User ..."); using (NorthwindEntities1 NWEntities1 = new NorthwindEntities1()) { Product1 product1 = (from p in NWEntities1.Product1s where p.ProductID == 3 select p).First(); Console.WriteLine("Original unit in stock: {0}", product1.UnitsInStock); product1.UnitsInStock += 2; Console.WriteLine("Current unit in stock to update: {0}", product1.UnitsInStock); NWEntities1.SaveChanges(); Console.WriteLine("update submitted to database"); } // first user is ready to submit changes Console.WriteLine("\nFirst User ..."); try { NWEntities.SaveChanges(); } catch (DbUpdateConcurrencyException e) { Console.WriteLine("Conflicts detected. Refreshing ..."); var entry = e.Entries.Single(); entry.OriginalValues.SetValues(entry.GetDatabaseValues()); NWEntities.SaveChanges(); Console.WriteLine("update submitted to database after refresh"); } } }