private OrderDetail CreateOrderDetail(EntityManager em, Order order, Product product) { var od = new OrderDetail(); var orderID = order.OrderID; var productID = product.ProductID; od.ProductID = productID; od.OrderID = orderID; od.Quantity = 1; od.UnitPrice = 3.14m; em.AddEntity(od); return od; }
public async Task UnattachedChildrenMultipartkey() { await _emTask; if (TestFns.DEBUG_MONGO) { Assert.Inconclusive("NA for Mongo - OrderDetail"); return; } var order = new Order(); order.OrderID = 999; for (int i = 0; i < 3; i++) { var od = new OrderDetail(); od.ProductID = i; od.OrderID = order.OrderID; _em1.AttachEntity(od); } _em1.AttachEntity(order); Assert.IsTrue(order.EntityAspect.EntityState.IsUnchanged(), "order should be unchanged"); Assert.IsTrue(order.OrderDetails.All(od => od.EntityAspect.EntityState.IsUnchanged()), "ods should all be unchanged"); Assert.IsTrue(order.OrderDetails.Count == 3, "should be 3 ods"); Assert.IsTrue(order.OrderDetails.All(od => od.Order == order), "should all point to order"); Assert.IsTrue(order.OrderDetails.All(od => od.OrderID == order.OrderID), "should all have correct orderId"); }
public async Task UnidirectionalAttachFk() { await _emTask; if (TestFns.DEBUG_MONGO) { Assert.Inconclusive("NA for Mongo - Order/OrderDetail"); return; } var od1 = new OrderDetail(); var prod1 = new Product(); od1.ProductID = -99; _em1.AttachEntity(od1); _em1.AttachEntity(prod1); Assert.IsTrue(od1.Product == null, "Product should be null"); prod1.ProductID = 2; od1.ProductID = 2; Assert.IsTrue(od1.Product == prod1, "should now point to product"); var od2 = new OrderDetail(); var prod2 = new Product(); od2.ProductID = -88; _em1.AttachEntity(od2); _em1.AttachEntity(prod2); Assert.IsTrue(od2.Product == null, "Product should be null - again"); // same as above but different order od2.ProductID = 3; // should now have an unresolved parent. prod2.ProductID = 3; Assert.IsTrue(od2.Product == prod2, "should now point to product - again"); }
public async Task AddEntityNoOrPartialKey() { await _emTask; if (TestFns.DEBUG_MONGO) { Assert.Inconclusive("NA for Mongo - OrderDetail"); return; } var od = new OrderDetail(); try { _em1.AttachEntity(od, EntityState.Added); Assert.Fail("should not get here"); } catch (Exception e) { Assert.IsTrue(e.Message.Contains("key"), "error message should contain 'key'"); } // only need to set part of the key od.OrderID = 999; _em1.AttachEntity(od, EntityState.Added); Assert.IsTrue(od.EntityAspect.EntityState.IsAdded()); try { var od2 =_em1.CreateEntity<OrderDetail>(EntityState.Added); Assert.Fail("should not get here"); } catch (Exception e) { Assert.IsTrue(e.Message.Contains("key"), "error message should contain 'key'"); } }
public async Task GraphAttachMultipartKey() { var em1 = await TestFns.NewEm(_serviceName); if (TestFns.DEBUG_MONGO) { Assert.Inconclusive("NA for Mongo - OrderDetail"); return; } var order = new Order(); order.OrderID = 999; for (int i = 0; i < 3; i++) { var od = new OrderDetail(); od.ProductID = i; order.OrderDetails.Add(od); } em1.AttachEntity(order); Assert.IsTrue(order.EntityAspect.EntityState.IsUnchanged(), "order should be unchanged"); Assert.IsTrue(order.OrderDetails.All(od => od.EntityAspect.EntityState.IsUnchanged()), "ods should all be unchanged"); Assert.IsTrue(order.OrderDetails.Count == 3, "should be 3 ods"); Assert.IsTrue(order.OrderDetails.All(od => od.Order == order), "should all point to order"); Assert.IsTrue(order.OrderDetails.All(od => od.OrderID == 999), "should all have correct orderId"); }