private static int TestWithTransaction() { var affectedRows = 0; var customerId = "RATTC"; var employeeId = 5; var invalidEmployeeId = 5000; using (var dbContext = new NorthwindEntities()) { using (var transaction = dbContext.Database.BeginTransaction()) { try { // To test with invalid data and see what happens, change employeeId with invalidEmployeeId // then the transaction will rollback and there will be no added orders var firstOrder = new Order() { CustomerID = customerId, EmployeeID = employeeId }; dbContext.Orders.Add(firstOrder); var secondOrder = new Order() { CustomerID = customerId, EmployeeID = employeeId }; dbContext.Orders.Add(secondOrder); affectedRows = dbContext.SaveChanges(); transaction.Commit(); Console.WriteLine("- Finish successfully => Commit transaction"); } catch (Exception) { transaction.Rollback(); Console.WriteLine("- Exception: Finish Unsuccessfully => Rollback transaction"); } } } return affectedRows; }
/// <summary> /// Started the transaction explicitly. We have options to commit / roll-back transactions. /// </summary> private static int TestOnExplicitlyStartedTransaction() { var affectedRows = 0; var customerId = "VINET"; var employeeId = 5; var invalidEmployeeId = int.MaxValue; using (var dbContext = new NorthwindEntities()) { using (var transaction = dbContext.Database.BeginTransaction()) { try { #region [Add Orders] // This cause an error var firstOrder = new Order() { CustomerID = customerId, EmployeeID = invalidEmployeeId // employeeId }; dbContext.Orders.Add(firstOrder); var secondOrder = new Order() { CustomerID = customerId, EmployeeID = employeeId }; secondOrder.Order_Details.Add(new Order_Detail() { OrderID = secondOrder.OrderID, ProductID = 5, UnitPrice = 12.34m, Quantity = 100, Discount = 0.2f }); dbContext.Orders.Add(secondOrder); #endregion affectedRows = dbContext.SaveChanges(); // Finish successfully => Commit transaction transaction.Commit(); // Test to Rollback() insted of Commit() and you will see that the changes are rolled-back //transaction.Rollback(); Console.WriteLine("- Finish successfully => Commit transaction"); } catch (Exception) { // Finish Unsuccessfully => Rollback transaction transaction.Rollback(); Console.WriteLine("- Exception: Finish Unsuccessfully => Rollback transaction"); } } } return affectedRows; }
/// <summary> /// Started the transaction implicitly. /// </summary> private static int TestOnImplicitlyStartedTransaction() { var affectedRows = 0; var customerId = "RATTC"; var employeeId = 6; var invalidEmployeeId = int.MaxValue; using (var dbContext = new NorthwindEntities()) { try { #region [Add Orders] // This cause an error var firstOrder = new Order() { CustomerID = customerId, EmployeeID = invalidEmployeeId // employeeId }; dbContext.Orders.Add(firstOrder); var secondOrder = new Order() { CustomerID = customerId, EmployeeID = employeeId }; secondOrder.Order_Details.Add(new Order_Detail() { OrderID = secondOrder.OrderID, ProductID = 5, UnitPrice = 12.34m, Quantity = 100, Discount = 0.2f }); dbContext.Orders.Add(secondOrder); #endregion affectedRows = dbContext.SaveChanges(); Console.WriteLine("- Finish successfully => Commit transaction"); } catch (Exception) { Console.WriteLine("- Exception: Finish Unsuccessfully => Rollback transaction"); } } return affectedRows; }
// Another useful utility method private Order CreateOrder(string shipName = "New Order") { var order = new Order(); order.ShipName = shipName; return order; }