/// <summary> /// Problem solved by only using 1 db connection or introducing transactions isolation levels /// </summary> public static void Main() { using (var db1 = new NorthwindEntities()) { using (var db2 = new NorthwindEntities()) { Console.WriteLine("First connection altering"); Console.WriteLine("Original -> " + db1.Employees.First().City); db1.Employees.First().City = "Changed city"; Console.WriteLine("Changed -> " + db1.Employees.First().City); Console.WriteLine("Second connection altering"); Console.WriteLine("Original -> " + db2.Employees.First().City); db2.Employees.First().City = "Altered city"; Console.WriteLine("Changed -> " + db2.Employees.First().City); db1.SaveChanges(); db2.SaveChanges(); } } using (var db3 = new NorthwindEntities()) { Console.WriteLine("Final connection altering"); Console.WriteLine(db3.Employees.First().City); } }
public static string AddCustomer(NorthwindEntities northwind, Customer customer) { northwind.Customers.Add(customer); northwind.SaveChanges(); return "Customer added"; }
public static void Main() { using (var northwind1 = new NorthwindEntities()) { TransactionOptions options = new TransactionOptions(); options.IsolationLevel = IsolationLevel.Serializable; using (var scope = new TransactionScope(TransactionScopeOption.Required, options)) { using (var northwind2 = new NorthwindEntities()) { Customer customer = northwind1.Customers.FirstOrDefault(); customer.Country = "NewCountry"; Customer customer2 = northwind2.Customers.FirstOrDefault(); northwind1.SaveChanges(); Console.WriteLine("First connection country ->" + customer.Country); Console.WriteLine("Second connection country ->" + customer2.Country); Console.WriteLine("The second connection works with the old data - dirty read."); scope.Complete(); } } } }
public static string DeleteCustomer(NorthwindEntities northwind, Customer newCustomer) { var selectedCustomer = northwind.Customers.Where(c => c.CustomerID == newCustomer.CustomerID).FirstOrDefault(); northwind.Customers.Remove(selectedCustomer); northwind.SaveChanges(); return "Customer deleted"; }
/// <summary> /// EF works with transactions by default - calling SaveChanges() starts a transaction by default. /// However we can use transactions to unify many calling of SaveChanges() as one and if one fails - all fail. /// </summary> public static void Main() { using (var db = new NorthwindEntities()) { using (var transaction = db.Database.BeginTransaction()) { var order = new Order() { CustomerID = "WELLI", EmployeeID = 4 }; db.Orders.Add(order); var orderDetails1 = new Order_Detail() { OrderID = order.OrderID, ProductID = 5, UnitPrice = 12.34m, Quantity = 100, Discount = 0.2f }; var orderDetails2 = new Order_Detail() { OrderID = order.OrderID, ProductID = 3, UnitPrice = 12.34m, Quantity = 100, Discount = 0.2f }; var orderDetails3 = new Order_Detail() { OrderID = order.OrderID, ProductID = 4, UnitPrice = 12.34m, Quantity = 100, Discount = 0.2f }; db.Order_Details.AddRange(new[] { orderDetails1, orderDetails2, orderDetails3 }); try { db.SaveChanges(); } catch (System.Exception ex) { System.Console.WriteLine(ex); transaction.Rollback(); } transaction.Commit(); } } }
public static int ModifyCustomer(string contactName, string companyName) { using (NorthwindEntities db = new NorthwindEntities()) { string id = GetCustomerId(companyName); var customer = db.Customers.Where(x => x.CustomerID == id).FirstOrDefault(); if (customer == null) throw new ArgumentException("The record you are trying to modify doesn't exist!!!"); customer.CompanyName = companyName; customer.ContactName = contactName; return db.SaveChanges(); } }
public static int DeleteCustomer(string companyName) { using (NorthwindEntities db = new NorthwindEntities()) { string id = GetCustomerId(companyName); if (IsCustomerExists(db, id)) { Customer customer = db.Customers.Where(x => x.CustomerID == id).FirstOrDefault(); db.Customers.Remove(customer); return db.SaveChanges(); } else { return 0; } } }
public static void Main() { using (var northwind = new NorthwindEntities()) { using (var scope = new TransactionScope()) { for (int i = 0; i < 10; i++) { northwind.Orders.Add(new Order() { ShipAddress = "455, Metropolitan Avenue" }); } northwind.SaveChanges(); scope.Complete(); } Console.WriteLine("Number of added orders: {0}", northwind.Orders.Where(o => o.ShipAddress == "455, Metropolitan Avenue").Count()); } }
public static int InsertCustomer(string contactName, string companyName) { using (NorthwindEntities db = new NorthwindEntities()) { Customer customer = new Customer() { ContactName = contactName, CompanyName = companyName, CustomerID = GetCustomerId(companyName), }; if (IsCustomerExists(db, GetCustomerId(companyName))) { throw new ArgumentException("Customer already exist!!!"); } db.Customers.Add(customer); return db.SaveChanges(); } }
public static string ModifyCustomerById(NorthwindEntities northwind, string customerId, Customer newCustomer) { var selectedCustomer = northwind.Customers.Where(c => c.CustomerID == customerId).FirstOrDefault(); selectedCustomer.Address = newCustomer.Address; selectedCustomer.City = newCustomer.City; selectedCustomer.CompanyName = newCustomer.CompanyName; selectedCustomer.ContactName = newCustomer.ContactName; selectedCustomer.ContactTitle = newCustomer.ContactTitle; selectedCustomer.Country = newCustomer.Country; selectedCustomer.CustomerDemographics = newCustomer.CustomerDemographics; selectedCustomer.Fax = newCustomer.Fax; selectedCustomer.Orders = newCustomer.Orders; selectedCustomer.Phone = newCustomer.Phone; selectedCustomer.PostalCode = newCustomer.PostalCode; selectedCustomer.Region = newCustomer.Region; northwind.SaveChanges(); return "Customer modified"; }
//TODO: 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? static void Main() { string companyName = "Concurency Test"; string contactName = "Concurent1"; string contactName2 = "Concurent2"; using (NorthwindEntities db = new NorthwindEntities()) { using(NorthwindEntities db2 = new NorthwindEntities()) { string id = GetCustomerId(companyName); var customer = db.Customers.Where(x => x.CustomerID == id).FirstOrDefault(); var customer2 = db2.Customers.Where(x => x.CustomerID == id).FirstOrDefault(); if (customer == null) throw new ArgumentException("The record you are trying to modify doesn't exist!!!"); customer.CompanyName = companyName; customer.ContactName = contactName; customer2.ContactName= contactName2; db.SaveChanges(); db2.SaveChanges(); } } }
private static void CreateOrder(Order newOrder, int numberOfRepeatsUntillSuccess) { bool isTransactionSucceed = false; using (NorthwindEntities dbContext = new NorthwindEntities()) { using (TransactionScope transaction = new TransactionScope()) { for (int i = 0; i < numberOfRepeatsUntillSuccess; i++) { try { dbContext.Orders.Add(newOrder); dbContext.SaveChanges(); transaction.Complete(); isTransactionSucceed = true; break; } catch (UpdateException ex) { if (i == numberOfRepeatsUntillSuccess - 1) { throw new InvalidOperationException("Cannot complete order creation", ex); } } } if (isTransactionSucceed) { // Reset the context since the operation succeeded. Console.WriteLine("Transaction sompited"); } else { Console.WriteLine("The operation could not be completed in " + numberOfRepeatsUntillSuccess + " tries."); } } } }
public static void AddOrder(NorthwindEntities northwind, Order order) { northwind.Orders.Add(order); northwind.SaveChanges(); }