private void ConflictOnDelete(Action <EnterpriseContext> save) { EnterpriseContext context = new EnterpriseContext(); // Add order1 Order order1 = new Order(); context.Orders.Add(order1); // Add orderDetail1 OrderDetail orderDetail1 = new OrderDetail(); orderDetail1.Order = order1; context.OrderDetails.Add(orderDetail1); // Add customer1 Customer customer1 = new Customer(); customer1.FirstName = "customer1"; context.Customers.Add(customer1); customer1.Orders.Add(order1); #region check order1.CheckAdded(); customer1.CheckAdded(); orderDetail1.CheckAdded(); context.Changes.CheckCount(3) .CheckFound(order1) .CheckFound(customer1) .CheckFound(orderDetail1); #endregion check // save changes context.Execute(save); #region check context.Changes.CheckCount(0); order1.CheckUnchanged(); customer1.CheckUnchanged(); orderDetail1.CheckUnchanged(); customer1.Orders.CheckCount(1).CheckItem(0, order1); order1.Customer.Check(customer1); order1.CustomerID.Check(customer1.ID); order1.OrderDetails.CheckCount(1).CheckItem(0, orderDetail1); orderDetail1.Order.Check(order1); orderDetail1.OrderID.Check(order1.ID); order1.GetChangedProperties().CheckCount(0); customer1.GetChangedProperties().CheckCount(0); orderDetail1.GetChangedProperties().CheckCount(0); #endregion check // Add shipment1 Shipment shipment1 = new Shipment(); context.Shipments.Add(shipment1); // Add order2 Order order2 = new Order(); context.Orders.Add(order2); // Modify orderDetail1 orderDetail1.Order = order2; // Add orderDetail2 OrderDetail orderDetail2 = new OrderDetail(); orderDetail2.Order = order2; context.OrderDetails.Add(orderDetail2); // Modify order1 order1.Customer = null; // Add customer2 Customer customer2 = new Customer(); customer2.FirstName = "customer2"; context.Customers.Add(customer2); // Modify customer1 customer1.SecondPartner = customer2; // Modify order1 order1.Customer = customer2; shipment1.Order = order2; // Add customer3 Customer customer3 = new Customer(); customer3.FirstName = "customer3"; context.Customers.Add(customer3); customer2.FirstPartner = customer3; // Delete customer1 context.Customers.Remove(customer1); order2.Customer = customer2; // Modify order1 order1.Customer = customer3; #region check order2.CheckAdded(); shipment1.CheckAdded(); customer2.CheckAdded(); customer3.CheckAdded(); orderDetail2.CheckAdded(); customer1.CheckDeleted(); order1.CheckModified(); order1.Customer.Check(customer3); order1.CustomerID.Check(customer3.ID); order1.GetChangedProperties().CheckCount(1).CheckFound <Order>(o => o.CustomerID); orderDetail1.CheckModified(); orderDetail1.Order.Check(order2); orderDetail1.OrderID.Check(order2.ID); orderDetail1.GetChangedProperties().CheckCount(1).CheckFound <OrderDetail>(od => od.OrderID); context.Changes.CheckCount(8) .CheckFound(order1) .CheckFound(order2) .CheckFound(shipment1) .CheckFound(customer1) .CheckFound(customer2) .CheckFound(customer3) .CheckFound(orderDetail1) .CheckFound(orderDetail2); #endregion check #region delete order1 async Task delete() { EntityDatabaseConnection connection = GetConnection(); await connection.OpenAsync(); connection.BeginTransaction(IsolationLevel.Snapshot); await Database.Connect(connection) .Prepared() .WithParameters(new { OrderID = order1.ID }) .Query(@"set nocount on; delete Orders where ID = @OrderID; waitfor delay '00:00:00.5' ") .ExecuteAsync() .ContinueWith(t => { if (!t.IsFaulted) { throw new Exception("Expected Exception, Was Not Thrown"); } connection.RollbackTransaction(); connection.Close(); }); } Task task = delete(); #endregion delete order1 // save changes context.Execute(save); task.Wait(); #region check order1.CheckUnchanged(); order2.CheckUnchanged(); shipment1.CheckUnchanged(); customer2.CheckUnchanged(); customer3.CheckUnchanged(); orderDetail1.CheckUnchanged(); orderDetail2.CheckUnchanged(); customer1.CheckDetached(); order1.Customer.Check(customer3); order1.CustomerID.Check(customer3.ID); order1.GetChangedProperties().CheckCount(0); order1.OrderDetails.CheckCount(0); order1.Shipment.CheckItem(null); order2.Customer.Check(customer2); order2.CustomerID.Check(customer2.ID); order2.GetChangedProperties().CheckCount(0); order2.OrderDetails.CheckCount(2).CheckItem(0, orderDetail1).CheckItem(1, orderDetail2); order2.Shipment.CheckItem(shipment1); shipment1.Order.Check(order2); shipment1.OrderID.Check(order2.ID); shipment1.GetChangedProperties().CheckCount(0); customer2.FirstPartner.Check(customer3); customer2.FirstPartnerID.Check(customer3.ID); customer2.SecondPartner.Check(null); customer2.SecondPartnerID.Check(null); customer2.GetChangedProperties().CheckCount(0); customer2.Orders.CheckCount(1).CheckItem(0, order2); customer2.FirstPartners.CheckCount(0); customer2.SecondPartners.CheckCount(0); customer3.FirstPartner.Check(null); customer3.FirstPartnerID.Check(null); customer3.SecondPartner.Check(null); customer3.SecondPartnerID.Check(null); customer2.GetChangedProperties().CheckCount(0); customer3.Orders.CheckCount(1).CheckItem(0, order1); customer3.FirstPartners.CheckCount(1).CheckItem(0, customer2); customer3.SecondPartners.CheckCount(0); orderDetail1.Order.Check(order2); orderDetail1.OrderID.Check(order2.ID); orderDetail1.GetChangedProperties().CheckCount(0); orderDetail2.Order.Check(order2); orderDetail2.OrderID.Check(order2.ID); orderDetail2.GetChangedProperties().CheckCount(0); context.Changes.CheckCount(0); #endregion check }
private void Conflict(Action <EnterpriseContext> save) { EnterpriseContext context = new EnterpriseContext(); #region step1: define some new data ... Order order = new Order(); order.ReceiveDate = "1398/06/25".ToDateTime(); context.Orders.Add(order); OrderDetail orderDetail = new OrderDetail(); orderDetail.Order = order; orderDetail.Amount = 100; context.OrderDetails.Add(orderDetail); Customer ali = new Customer(); ali.FirstName = "Ali"; ali.LastName = "Zare"; context.Customers.Add(ali); order.Customer = ali; Product laptop = new Product(); laptop.Name = "Laptop Asus N552"; context.Products.Add(laptop); orderDetail.Product = laptop; #region check laptop.CheckAdded(); ali.CheckAdded(); ali.Orders.CheckCount(1).CheckItem(0, order); order.CheckAdded(); order.CustomerID.Check(ali.ID); order.OrderDetails.CheckCount(1).CheckItem(0, orderDetail); orderDetail.CheckAdded(); orderDetail.OrderID.Check(order.ID); orderDetail.ProductID.Check(laptop.ID); context.Changes.CheckCount(4) .CheckFound(laptop) .CheckFound(ali) .CheckFound(order) .CheckFound(orderDetail); #endregion check // save changes context.Execute(save); #region check laptop.CheckUnchanged(); ali.CheckUnchanged(); ali.Orders.CheckCount(1).CheckItem(0, order); order.CheckUnchanged(); order.Customer.Check(ali); order.CustomerID.Check(ali.ID); order.OrderDetails.CheckCount(1).CheckItem(0, orderDetail); orderDetail.CheckUnchanged(); orderDetail.Order.Check(order); orderDetail.OrderID.Check(order.ID); orderDetail.Product.Check(laptop); orderDetail.ProductID.Check(laptop.ID); context.Changes.CheckCount(0); #endregion check #endregion step1: define some new data ... // Test Snapshot ... #region step2: define some new data and edit previous data ... Order order1 = new Order(); order1.Customer = ali; order1.ReceiveDate = "1398/07/07".ToDateTime(); context.Orders.Add(order1); OrderDetail orderDetail1 = new OrderDetail(); orderDetail1.Order = order1; orderDetail1.Amount = 500; context.OrderDetails.Add(orderDetail1); Customer amir = new Customer(); amir.FirstName = "Amir"; amir.LastName = "Zare"; context.Customers.Add(amir); order.Customer = amir; Product tv = new Product(); tv.Name = "Sony NX 40 inch"; context.Products.Add(tv); orderDetail.Product = tv; orderDetail1.Product = laptop; #region check laptop.CheckUnchanged(); ali.CheckUnchanged(); ali.Orders.CheckCount(1).CheckItem(0, order1); order.CheckModified(); order.CustomerID.Check(amir.ID); order.OrderDetails.CheckCount(1).CheckItem(0, orderDetail); order.GetChangedProperties().CheckCount(1).CheckFound <Order>(o => o.CustomerID); orderDetail.CheckModified(); orderDetail.ProductID.Check(tv.ID); orderDetail.GetChangedProperties().CheckCount(1).CheckFound <OrderDetail>(od => od.ProductID); tv.CheckAdded(); amir.CheckAdded(); amir.Orders.CheckCount(1).CheckItem(0, order); order1.CheckAdded(); order1.CustomerID.Check(ali.ID); order1.OrderDetails.CheckCount(1).CheckItem(0, orderDetail1); orderDetail1.CheckAdded(); orderDetail1.OrderID.Check(order1.ID); orderDetail1.ProductID.Check(laptop.ID); context.Changes.CheckCount(6) .CheckFound(tv) .CheckFound(amir) .CheckFound(order) .CheckFound(order1) .CheckFound(orderDetail) .CheckFound(orderDetail1); #endregion check bool has_concurrency_conflict = false; #region parallel delete EntityDatabaseConnection connection = GetConnection(); connection.Open(); Database.Connect(connection) .Prepared() .WithParameters(new { OrderID = order.ID }) .Query(@"set nocount on; delete from OrderDetails where OrderID = @OrderID delete from Orders where ID = @OrderID") .Execute(); connection.Close(); #endregion parallel delete try { // save changes context.Execute(save); } catch (EntityDatabaseConcurrencyException) { has_concurrency_conflict = true; } #region check if (has_concurrency_conflict) { laptop.CheckUnchanged(); ali.CheckUnchanged(); ali.Orders.CheckCount(1).CheckItem(0, order1); order.CheckModified(); order.CustomerID.Check(amir.ID); order.OrderDetails.CheckCount(1).CheckItem(0, orderDetail); order.GetChangedProperties().CheckCount(1).CheckFound <Order>(o => o.CustomerID); orderDetail.CheckModified(); orderDetail.ProductID.Check(tv.ID); orderDetail.GetChangedProperties().CheckCount(1).CheckFound <OrderDetail>(od => od.ProductID); tv.CheckAdded(); amir.CheckAdded(); amir.Orders.CheckCount(1).CheckItem(0, order); order1.CheckAdded(); order1.CustomerID.Check(ali.ID); order1.OrderDetails.CheckCount(1).CheckItem(0, orderDetail1); orderDetail1.CheckAdded(); orderDetail1.OrderID.Check(order1.ID); orderDetail1.ProductID.Check(laptop.ID); context.Changes.CheckCount(6) .CheckFound(tv) .CheckFound(amir) .CheckFound(order) .CheckFound(order1) .CheckFound(orderDetail) .CheckFound(orderDetail1); } else { throw new Exception("Expected Exception, Was Not Thrown"); #region not reachable code tv.CheckUnchanged(); laptop.CheckUnchanged(); ali.CheckUnchanged(); ali.Orders.CheckCount(1).CheckItem(0, order1); amir.CheckUnchanged(); amir.Orders.CheckCount(1).CheckItem(0, order); order.CheckUnchanged(); order.Customer.Check(amir); order.CustomerID.Check(amir.ID); order.OrderDetails.CheckCount(1).CheckItem(0, orderDetail); order1.CheckUnchanged(); order1.Customer.Check(ali); order1.CustomerID.Check(ali.ID); order1.OrderDetails.CheckCount(1).CheckItem(0, orderDetail1); orderDetail.CheckUnchanged(); orderDetail.Order.Check(order); orderDetail.OrderID.Check(order.ID); orderDetail.Product.Check(tv); orderDetail.ProductID.Check(tv.ID); orderDetail1.CheckUnchanged(); orderDetail1.Order.Check(order1); orderDetail1.OrderID.Check(order1.ID); orderDetail1.Product.Check(laptop); orderDetail1.ProductID.Check(laptop.ID); context.Changes.CheckCount(0); #endregion not reachable code } #endregion check #endregion step2: define some new data and edit previous data ... }
private void ConflictOnUpdate(Action <EnterpriseContext> save) { EnterpriseContext context = new EnterpriseContext(); // Add order1 Order order1 = new Order(); context.Orders.Add(order1); // Add orderDetail1 OrderDetail orderDetail1 = new OrderDetail(); orderDetail1.Order = order1; context.OrderDetails.Add(orderDetail1); // Add customer1 Customer customer1 = new Customer(); customer1.FirstName = "customer1"; context.Customers.Add(customer1); customer1.Orders.Add(order1); #region check order1.CheckAdded(); customer1.CheckAdded(); orderDetail1.CheckAdded(); context.Changes.CheckCount(3) .CheckFound(order1) .CheckFound(customer1) .CheckFound(orderDetail1); #endregion check // save changes context.Execute(save); #region check context.Changes.CheckCount(0); order1.CheckUnchanged(); customer1.CheckUnchanged(); orderDetail1.CheckUnchanged(); customer1.Orders.CheckCount(1).CheckItem(0, order1); order1.Customer.Check(customer1); order1.CustomerID.Check(customer1.ID); order1.OrderDetails.CheckCount(1).CheckItem(0, orderDetail1); orderDetail1.Order.Check(order1); orderDetail1.OrderID.Check(order1.ID); order1.GetChangedProperties().CheckCount(0); customer1.GetChangedProperties().CheckCount(0); orderDetail1.GetChangedProperties().CheckCount(0); #endregion check // Add shipment1 Shipment shipment1 = new Shipment(); context.Shipments.Add(shipment1); // Add order2 Order order2 = new Order(); context.Orders.Add(order2); // Modify orderDetail1 orderDetail1.Order = order2; // Add orderDetail2 OrderDetail orderDetail2 = new OrderDetail(); orderDetail2.Order = order2; context.OrderDetails.Add(orderDetail2); // Modify order1 order1.Customer = null; // Add customer2 Customer customer2 = new Customer(); customer2.FirstName = "customer2"; context.Customers.Add(customer2); // Modify customer1 customer1.SecondPartner = customer2; // Modify order1 order1.Customer = customer2; shipment1.Order = order2; // Add customer3 Customer customer3 = new Customer(); customer3.FirstName = "customer3"; context.Customers.Add(customer3); customer2.FirstPartner = customer3; // Delete customer1 context.Customers.Remove(customer1); order2.Customer = customer2; // Modify order1 order1.Customer = customer3; #region check order2.CheckAdded(); shipment1.CheckAdded(); customer2.CheckAdded(); customer3.CheckAdded(); orderDetail2.CheckAdded(); customer1.CheckDeleted(); order1.CheckModified(); order1.Customer.Check(customer3); order1.CustomerID.Check(customer3.ID); order1.GetChangedProperties().CheckCount(1).CheckFound <Order>(o => o.CustomerID); orderDetail1.CheckModified(); orderDetail1.Order.Check(order2); orderDetail1.OrderID.Check(order2.ID); orderDetail1.GetChangedProperties().CheckCount(1).CheckFound <OrderDetail>(od => od.OrderID); context.Changes.CheckCount(8) .CheckFound(order1) .CheckFound(order2) .CheckFound(shipment1) .CheckFound(customer1) .CheckFound(customer2) .CheckFound(customer3) .CheckFound(orderDetail1) .CheckFound(orderDetail2); #endregion check #region update order1 async Task update() { EntityDatabaseConnection connection = GetConnection(); await connection.OpenAsync(); connection.BeginTransaction(IsolationLevel.Snapshot); await Database.Connect(connection) .Prepared() .WithParameters(new { OrderID = order1.ID, ShippingDate = "1398/06/30".ToDateTime() }) .Query(@"set nocount on; update Orders set ShippingDate = @ShippingDate where ID = @OrderID; waitfor delay '00:00:00.5' ") .ExecuteAsync(); connection.CommitTransaction(); connection.Close(); } Task task = update(); #endregion update order1 try { // save changes context.Execute(save); } catch (SqlException e) when(e.Number == 3960) { // it's ok, conflict was occurred } task.Wait(); #region check order2.CheckAdded(); shipment1.CheckAdded(); customer2.CheckAdded(); customer3.CheckAdded(); orderDetail2.CheckAdded(); customer1.CheckDeleted(); order1.CheckModified(); order1.Customer.Check(customer3); order1.CustomerID.Check(customer3.ID); order1.GetChangedProperties().CheckCount(1).CheckFound <Order>(o => o.CustomerID); orderDetail1.CheckModified(); orderDetail1.Order.Check(order2); orderDetail1.OrderID.Check(order2.ID); orderDetail1.GetChangedProperties().CheckCount(1).CheckFound <OrderDetail>(od => od.OrderID); context.Changes.CheckCount(8) .CheckFound(order1) .CheckFound(order2) .CheckFound(shipment1) .CheckFound(customer1) .CheckFound(customer2) .CheckFound(customer3) .CheckFound(orderDetail1) .CheckFound(orderDetail2); #endregion check }
private void Data(Action <EnterpriseContext> save) { EnterpriseContext context = new EnterpriseContext(); Customer ali = new Customer(); ali.FirstName = "Ali"; ali.LastName = "Zare"; context.Customers.Add(ali); Customer amir = new Customer(); amir.FirstName = "Amir"; context.Customers.Add(amir); Customer sam = new Customer(); context.Customers.Add(sam); Order order1 = new Order(); order1.ReceiveDate = "1398/05/06".ToDateTime(); order1.ShippingDate = "1398/06/06".ToDateTime(); context.Orders.Add(order1); Order order2 = new Order(); context.Orders.Add(order2); Order order3 = new Order(); context.Orders.Add(order3); #region insert amir, sam, order2 and order3 using (EntityDatabaseConnection connection = GetConnection()) { connection.Open(); EntityDatabase <EnterpriseContext> .Connect(connection) .Table <Customer>().Insert().WithCallBack(e => new { e.RV }) .Write(amir, sam); EntityDatabase <EnterpriseContext> .Connect(connection) .Table <Order>().Insert().WithCallBack(e => new { e.RV }) .Write(order2, order3); connection.Close(); } #endregion insert amir, sam, order2 and order3 amir.LastName = "Zare"; context.Customers.Remove(sam); order2.CustomerID = ali.ID; context.Orders.Remove(order3); #region check ali.CheckAdded(); amir.CheckModified(); sam.CheckDeleted(); order1.CheckAdded(); order2.CheckModified(); order3.CheckDeleted(); context.Changes.CheckCount(6) .CheckFound(ali) .CheckFound(amir) .CheckFound(sam) .CheckFound(order1) .CheckFound(order2) .CheckFound(order3); #endregion check bool has_concurrency_conflict = false; try { context.Execute(save); } catch (EntityDatabaseConcurrencyException) { has_concurrency_conflict = true; } #region check if (has_concurrency_conflict) { ali.CheckAdded(); amir.CheckModified(); sam.CheckDeleted(); order1.CheckAdded(); order2.CheckModified(); order3.CheckDeleted(); context.Changes.CheckCount(6) .CheckFound(ali) .CheckFound(amir) .CheckFound(sam) .CheckFound(order1) .CheckFound(order2) .CheckFound(order3); } else { ali.CheckUnchanged(); amir.CheckUnchanged(); sam.CheckDetached(); order1.CheckUnchanged(); order2.CheckUnchanged(); order3.CheckDetached(); context.Changes.CheckCount(0); } #endregion check }
private void Overflow(Action <EnterpriseContext> save) { EnterpriseContext context = new EnterpriseContext(); // Add order1 Order order1 = new Order(); context.Orders.Add(order1); // Add orderDetail1 OrderDetail orderDetail1 = new OrderDetail(); orderDetail1.Order = order1; context.OrderDetails.Add(orderDetail1); // Add customer1 Customer customer1 = new Customer(); customer1.FirstName = "customer1"; context.Customers.Add(customer1); customer1.Orders.Add(order1); #region check order1.CheckAdded(); customer1.CheckAdded(); orderDetail1.CheckAdded(); context.Changes.CheckCount(3) .CheckFound(order1) .CheckFound(customer1) .CheckFound(orderDetail1); #endregion check // save changes context.Execute(save); #region check context.Changes.CheckCount(0); order1.CheckUnchanged(); customer1.CheckUnchanged(); orderDetail1.CheckUnchanged(); customer1.Orders.CheckCount(1).CheckItem(0, order1); order1.Customer.Check(customer1); order1.CustomerID.Check(customer1.ID); order1.OrderDetails.CheckCount(1).CheckItem(0, orderDetail1); orderDetail1.Order.Check(order1); orderDetail1.OrderID.Check(order1.ID); order1.GetChangedProperties().CheckCount(0); customer1.GetChangedProperties().CheckCount(0); orderDetail1.GetChangedProperties().CheckCount(0); #endregion check // Add shipment1 Shipment shipment1 = new Shipment(); context.Shipments.Add(shipment1); // Add order2 Order order2 = new Order(); context.Orders.Add(order2); // Modify orderDetail1 orderDetail1.Order = order2; // Add orderDetail2 OrderDetail orderDetail2 = new OrderDetail(); orderDetail2.Order = order2; context.OrderDetails.Add(orderDetail2); // Modify order1 order1.Customer = null; // Add customer2 Customer customer2 = new Customer(); customer2.FirstName = "customer2"; context.Customers.Add(customer2); // Modify customer1 customer1.SecondPartner = customer2; // Modify order1 order1.Customer = customer2; shipment1.Order = order2; // Add customer3 Customer customer3 = new Customer(); customer3.FirstName = "customer3"; context.Customers.Add(customer3); customer2.FirstPartner = customer3; // Delete customer1 context.Customers.Remove(customer1); order2.Customer = customer2; // Modify order1 order1.Customer = customer3; #region check order2.CheckAdded(); shipment1.CheckAdded(); customer2.CheckAdded(); customer3.CheckAdded(); orderDetail2.CheckAdded(); customer1.CheckDeleted(); order1.CheckModified(); order1.Customer.Check(customer3); order1.CustomerID.Check(customer3.ID); order1.GetChangedProperties().CheckCount(1).CheckFound <Order>(o => o.CustomerID); orderDetail1.CheckModified(); orderDetail1.Order.Check(order2); orderDetail1.OrderID.Check(order2.ID); orderDetail1.GetChangedProperties().CheckCount(1).CheckFound <OrderDetail>(od => od.OrderID); context.Changes.CheckCount(8) .CheckFound(order1) .CheckFound(order2) .CheckFound(shipment1) .CheckFound(customer1) .CheckFound(customer2) .CheckFound(customer3) .CheckFound(orderDetail1) .CheckFound(orderDetail2); #endregion check Truncate_And_Insert(); try { // save changes context.Execute(save); } catch (SqlException e) when(e.Number == 8115) // arithmetic overflow error { Truncate_And_ReseedIdentity(); } #region check order2.CheckAdded(); shipment1.CheckAdded(); customer2.CheckAdded(); customer3.CheckAdded(); orderDetail2.CheckAdded(); customer1.CheckDeleted(); order1.CheckModified(); order1.Customer.Check(customer3); order1.CustomerID.Check(customer3.ID); order1.GetChangedProperties().CheckCount(1).CheckFound <Order>(o => o.CustomerID); orderDetail1.CheckModified(); orderDetail1.Order.Check(order2); orderDetail1.OrderID.Check(order2.ID); orderDetail1.GetChangedProperties().CheckCount(1).CheckFound <OrderDetail>(od => od.OrderID); context.Changes.CheckCount(8) .CheckFound(order1) .CheckFound(order2) .CheckFound(shipment1) .CheckFound(customer1) .CheckFound(customer2) .CheckFound(customer3) .CheckFound(orderDetail1) .CheckFound(orderDetail2); #endregion check }
private void ConcurrencyOnDelete(Action <EnterpriseContext> save, bool hasConcurrency, bool hasConflict) { EnterpriseContext context = new EnterpriseContext(); // Add order1 Order order1 = new Order(); context.Orders.Add(order1); // Add orderDetail1 OrderDetail orderDetail1 = new OrderDetail(); orderDetail1.Order = order1; context.OrderDetails.Add(orderDetail1); // Add customer1 Customer customer1 = new Customer(); customer1.FirstName = "customer1"; context.Customers.Add(customer1); customer1.Orders.Add(order1); #region check order1.CheckAdded(); customer1.CheckAdded(); orderDetail1.CheckAdded(); context.Changes.CheckCount(3) .CheckFound(order1) .CheckFound(customer1) .CheckFound(orderDetail1); #endregion check // save changes context.Execute(save); #region check context.Changes.CheckCount(0); order1.CheckUnchanged(); customer1.CheckUnchanged(); orderDetail1.CheckUnchanged(); customer1.Orders.CheckCount(1).CheckItem(0, order1); order1.Customer.Check(customer1); order1.CustomerID.Check(customer1.ID); order1.OrderDetails.CheckCount(1).CheckItem(0, orderDetail1); orderDetail1.Order.Check(order1); orderDetail1.OrderID.Check(order1.ID); order1.GetChangedProperties().CheckCount(0); customer1.GetChangedProperties().CheckCount(0); orderDetail1.GetChangedProperties().CheckCount(0); #endregion check // Add shipment1 Shipment shipment1 = new Shipment(); context.Shipments.Add(shipment1); // Add order2 Order order2 = new Order(); context.Orders.Add(order2); // Modify orderDetail1 orderDetail1.Order = order2; // Add orderDetail2 OrderDetail orderDetail2 = new OrderDetail(); orderDetail2.Order = order2; context.OrderDetails.Add(orderDetail2); // Modify order1 order1.Customer = null; // Add customer2 Customer customer2 = new Customer(); customer2.FirstName = "customer2"; context.Customers.Add(customer2); // Modify customer1 customer1.SecondPartner = customer2; // Modify order1 order1.Customer = customer2; shipment1.Order = order2; // Add customer3 Customer customer3 = new Customer(); customer3.FirstName = "customer3"; context.Customers.Add(customer3); customer2.FirstPartner = customer3; // Delete customer1 context.Customers.Remove(customer1); order2.Customer = customer2; // Modify order1 order1.Customer = customer3; #region check order2.CheckAdded(); shipment1.CheckAdded(); customer2.CheckAdded(); customer3.CheckAdded(); orderDetail2.CheckAdded(); customer1.CheckDeleted(); order1.CheckModified(); order1.Customer.Check(customer3); order1.CustomerID.Check(customer3.ID); order1.GetChangedProperties().CheckCount(1).CheckFound <Order>(o => o.CustomerID); orderDetail1.CheckModified(); orderDetail1.Order.Check(order2); orderDetail1.OrderID.Check(order2.ID); orderDetail1.GetChangedProperties().CheckCount(1).CheckFound <OrderDetail>(od => od.OrderID); context.Changes.CheckCount(8) .CheckFound(order1) .CheckFound(order2) .CheckFound(shipment1) .CheckFound(customer1) .CheckFound(customer2) .CheckFound(customer3) .CheckFound(orderDetail1) .CheckFound(orderDetail2); #endregion check #region delete orderDetail1 & order1 void delete() { EntityDatabaseConnection connection = GetConnection(); connection.Open(); Database.Connect(connection) .Prepared() .WithParameters(new { OrderDetailID = orderDetail1.ID, OrderID = order1.ID }) .Query(@"set nocount on; delete from OrderDetails where ID = @OrderDetailID delete from Orders where ID = @OrderID ") .Execute(); connection.Close(); } delete(); #endregion delete orderDetail1 & order1 try { // save changes context.Execute(save); } catch (EntityDatabaseConcurrencyException) when(hasConcurrency && !hasConflict) { // it's ok, concurrency was occurred } catch (EntityDatabaseConcurrencyException e) when(hasConcurrency && hasConflict && ((SqlException)e.InnerException).Number == 547) { // it's ok, concurrency was occurred } #region check if (hasConcurrency) { order2.CheckAdded(); shipment1.CheckAdded(); customer2.CheckAdded(); customer3.CheckAdded(); orderDetail2.CheckAdded(); customer1.CheckDeleted(); order1.CheckModified(); order1.Customer.Check(customer3); order1.CustomerID.Check(customer3.ID); order1.GetChangedProperties().CheckCount(1).CheckFound <Order>(o => o.CustomerID); orderDetail1.CheckModified(); orderDetail1.Order.Check(order2); orderDetail1.OrderID.Check(order2.ID); orderDetail1.GetChangedProperties().CheckCount(1).CheckFound <OrderDetail>(od => od.OrderID); context.Changes.CheckCount(8) .CheckFound(order1) .CheckFound(order2) .CheckFound(shipment1) .CheckFound(customer1) .CheckFound(customer2) .CheckFound(customer3) .CheckFound(orderDetail1) .CheckFound(orderDetail2); } else { order1.CheckUnchanged(); order2.CheckUnchanged(); shipment1.CheckUnchanged(); customer2.CheckUnchanged(); customer3.CheckUnchanged(); orderDetail1.CheckUnchanged(); orderDetail2.CheckUnchanged(); customer1.CheckDetached(); order1.Customer.Check(customer3); order1.CustomerID.Check(customer3.ID); order1.GetChangedProperties().CheckCount(0); order1.OrderDetails.CheckCount(0); order1.Shipment.CheckItem(null); order2.Customer.Check(customer2); order2.CustomerID.Check(customer2.ID); order2.GetChangedProperties().CheckCount(0); order2.OrderDetails.CheckCount(2).CheckItem(0, orderDetail1).CheckItem(1, orderDetail2); order2.Shipment.CheckItem(shipment1); shipment1.Order.Check(order2); shipment1.OrderID.Check(order2.ID); shipment1.GetChangedProperties().CheckCount(0); customer2.FirstPartner.Check(customer3); customer2.FirstPartnerID.Check(customer3.ID); customer2.SecondPartner.Check(null); customer2.SecondPartnerID.Check(null); customer2.GetChangedProperties().CheckCount(0); customer2.Orders.CheckCount(1).CheckItem(0, order2); customer2.FirstPartners.CheckCount(0); customer2.SecondPartners.CheckCount(0); customer3.FirstPartner.Check(null); customer3.FirstPartnerID.Check(null); customer3.SecondPartner.Check(null); customer3.SecondPartnerID.Check(null); customer2.GetChangedProperties().CheckCount(0); customer3.Orders.CheckCount(1).CheckItem(0, order1); customer3.FirstPartners.CheckCount(1).CheckItem(0, customer2); customer3.SecondPartners.CheckCount(0); orderDetail1.Order.Check(order2); orderDetail1.OrderID.Check(order2.ID); orderDetail1.GetChangedProperties().CheckCount(0); orderDetail2.Order.Check(order2); orderDetail2.OrderID.Check(order2.ID); orderDetail2.GetChangedProperties().CheckCount(0); context.Changes.CheckCount(0); } #endregion check }
private void Challenge(Action <EnterpriseContext> save) { Stopwatch sw = new Stopwatch(); sw.Start(); Product[] products = new Product[100]; Customer[] customers = new Customer[10]; Order[] orders = new Order[customers.Length * 10]; OrderDetail[] orderDetails = new OrderDetail[orders.Length * 10]; sw.Stop(); TestContext.WriteLine($"Entity Creation Time : {sw.ElapsedMilliseconds}"); sw.Restart(); for (int i = 0; i < products.Length; i++) { products[i] = new Product { Name = $"Product-{i:D6}" } } ; for (int i = 0; i < customers.Length; i++) { customers[i] = new Customer { FirstName = $"Customer-{i:D6}", LastName = $"Customer-{i:D6}", NationalIdentityNumber = $"9999{i:D6}" } } ; for (int i = 0; i < orders.Length; i++) { orders[i] = new Order { Customer = customers[i % customers.Length] } } ; for (int i = 0; i < orderDetails.Length; i++) { orderDetails[i] = new OrderDetail { Order = orders[i % orders.Length], Product = products[i % products.Length] } } ; sw.Stop(); TestContext.WriteLine($"Entity Initialize Time : {sw.ElapsedMilliseconds}"); sw.Restart(); EnterpriseContext context = new EnterpriseContext(); sw.Stop(); TestContext.WriteLine($"Context Creation Time : {sw.ElapsedMilliseconds}"); sw.Restart(); for (int i = 0; i < products.Length; i++) { context.Products.Add(products[i]); } for (int i = 0; i < customers.Length; i++) { context.Customers.Add(customers[i]); } for (int i = 0; i < orders.Length; i++) { context.Orders.Add(orders[i]); } for (int i = 0; i < orderDetails.Length; i++) { context.OrderDetails.Add(orderDetails[i]); } sw.Stop(); TestContext.WriteLine($"Add To Context Time : {sw.ElapsedMilliseconds}"); sw.Restart(); context.Execute(save); sw.Stop(); TestContext.WriteLine($"Insert To Database Time : {sw.ElapsedMilliseconds}"); sw.Restart(); for (int i = 0; i < customers.Length; i++) { customers[i].LastName = $"Modified-{customers[i].LastName}"; } for (int i = 0; i < orders.Length; i++) { orders[i].Customer = customers[(i + customers.Length - 1) % (customers.Length - 1)]; } for (int i = 0; i < orderDetails.Length; i++) { orderDetails[i].Product = products[(i + products.Length - 1) % (products.Length - 1)]; } sw.Stop(); TestContext.WriteLine($"Modify Entity Time : {sw.ElapsedMilliseconds}"); sw.Restart(); context.Execute(save); sw.Stop(); TestContext.WriteLine($"Update Database Time : {sw.ElapsedMilliseconds}"); sw.Restart(); for (int i = 0; i < orderDetails.Length; i++) { context.OrderDetails.Remove(orderDetails[i]); } for (int i = 0; i < orders.Length; i++) { context.Orders.Remove(orders[i]); } for (int i = 0; i < customers.Length; i++) { context.Customers.Remove(customers[i]); } for (int i = 0; i < products.Length; i++) { context.Products.Remove(products[i]); } sw.Stop(); TestContext.WriteLine($"Remove Entity Time : {sw.ElapsedMilliseconds}"); sw.Restart(); context.Execute(save); sw.Stop(); TestContext.WriteLine($"Delete From Database Time : {sw.ElapsedMilliseconds}"); }
private void Hierarchical(Action <EnterpriseContext> save) { EnterpriseContext context = new EnterpriseContext(); Order order = new Order(); context.Orders.Add(order); Customer customer5 = new Customer(); customer5.FirstName = "customer5"; context.Customers.Add(customer5); Customer customer6 = new Customer(); customer6.FirstName = "customer6"; context.Customers.Add(customer6); Customer customer3 = new Customer(); customer3.FirstName = "customer3"; context.Customers.Add(customer3); Customer customer1 = new Customer(); customer1.FirstName = "customer1"; context.Customers.Add(customer1); Customer customer2 = new Customer(); customer2.FirstName = "customer2"; context.Customers.Add(customer2); Customer customer4 = new Customer(); customer4.FirstName = "customer4"; context.Customers.Add(customer4); order.Customer = customer1; customer1.FirstPartner = customer2; customer1.SecondPartner = customer5; customer2.FirstPartner = customer3; customer2.SecondPartner = customer6; customer3.FirstPartner = customer4; customer5.FirstPartner = customer6; #region check order.CheckAdded(); customer1.CheckAdded(); customer2.CheckAdded(); customer3.CheckAdded(); customer4.CheckAdded(); customer5.CheckAdded(); customer6.CheckAdded(); context.Changes.CheckCount(7) .CheckFound(order) .CheckFound(customer1) .CheckFound(customer2) .CheckFound(customer3) .CheckFound(customer4) .CheckFound(customer5) .CheckFound(customer6); #endregion check // save changes context.Execute(save); #region check context.Changes.CheckCount(0); order.CheckUnchanged(); customer1.CheckUnchanged(); customer2.CheckUnchanged(); customer3.CheckUnchanged(); customer4.CheckUnchanged(); customer5.CheckUnchanged(); customer6.CheckUnchanged(); order.Customer.Check(customer1); order.CustomerID.Check(customer1.ID); customer1.FirstPartner.Check(customer2); customer1.FirstPartnerID.Check(customer2.ID); customer1.SecondPartner.Check(customer5); customer1.SecondPartnerID.Check(customer5.ID); customer2.FirstPartner.Check(customer3); customer2.FirstPartnerID.Check(customer3.ID); customer2.SecondPartner.Check(customer6); customer2.SecondPartnerID.Check(customer6.ID); customer3.FirstPartner.Check(customer4); customer3.FirstPartnerID.Check(customer4.ID); customer5.FirstPartner.Check(customer6); customer5.FirstPartnerID.Check(customer6.ID); #endregion check // Modify order order.Customer = null; order.CheckModified(); order.Customer.Check(null); order.CustomerID.Check(null); order.GetChangedProperties().CheckCount(1).CheckFound <Order>(o => o.CustomerID); // Delete customer1 context.Customers.Remove(customer1); customer1.CheckDeleted(); // Add customer7 Customer customer7 = new Customer(); customer7.FirstName = "customer7"; context.Customers.Add(customer7); customer7.CheckAdded(); // Modify order order.Customer = customer7; #region check order.CheckModified(); order.Customer.Check(customer7); order.CustomerID.Check(customer7.ID); order.GetChangedProperties().CheckCount(1).CheckFound <Order>(o => o.CustomerID); context.Changes.CheckCount(3) .CheckFound(order) .CheckFound(customer1) .CheckFound(customer7); // Modify customer2 customer2.FirstPartner = customer4; customer2.CheckModified(); customer2.GetChangedProperties().CheckCount(1).CheckFound <Customer>(o => o.FirstPartnerID); // Modify customer5 customer5.FirstPartner = customer3; customer5.CheckModified(); customer5.GetChangedProperties().CheckCount(1).CheckFound <Customer>(o => o.FirstPartnerID); // Modify customer4 customer4.FirstPartner = customer5; customer4.CheckModified(); customer4.GetChangedProperties().CheckCount(1).CheckFound <Customer>(o => o.FirstPartnerID); // Modify customer5 customer5.SecondPartner = customer2; customer5.CheckModified(); customer5.GetChangedProperties().CheckCount(2).CheckFound <Customer>(o => o.FirstPartnerID) .CheckFound <Customer>(o => o.SecondPartnerID); #endregion check // save changes context.Execute(save); #region check context.Changes.CheckCount(0); order.CheckUnchanged(); customer1.CheckDetached(); customer2.CheckUnchanged(); customer4.CheckUnchanged(); customer5.CheckUnchanged(); order.Customer.Check(customer7); order.CustomerID.Check(customer7.ID); customer2.FirstPartner.Check(customer4); customer2.FirstPartnerID.Check(customer4.ID); customer4.FirstPartner.Check(customer5); customer4.FirstPartnerID.Check(customer5.ID); customer5.FirstPartner.Check(customer3); customer5.FirstPartnerID.Check(customer3.ID); customer5.SecondPartner.Check(customer2); customer5.SecondPartnerID.Check(customer2.ID); #endregion check }