예제 #1
0
        public void TestCollectionDeleteAll()
        {
            int     prdId = -1;
            Product prd   = new Product();

            prd.es.Connection.Name = "ForeignKeyTest";

            try
            {
                using (esTransactionScope scope = new esTransactionScope())
                {
                    prd.ProductName  = "UnitTest";
                    prd.UnitPrice    = 1;
                    prd.Discontinued = false;
                    for (int i = 0; i < 3; i++)
                    {
                        OrderItem oi = prd.OrderItemCollectionByProductID.AddNew();
                        oi.OrderID   = i + 1;
                        oi.UnitPrice = prd.UnitPrice;
                        oi.Quantity  = Convert.ToInt16(i);
                        oi.Discount  = 0;
                    }
                    prd.Save();
                    prdId = prd.ProductID.Value;

                    // Test
                    ProductCollection collection = new ProductCollection();
                    collection.es.Connection.Name = "ForeignKeyTest";

                    Assert.IsTrue(collection.LoadAll());
                    Product entity = collection.FindByPrimaryKey(prdId);
                    Assert.AreEqual(3, entity.OrderItemCollectionByProductID.Count);
                    entity.OrderItemCollectionByProductID.MarkAllAsDeleted();
                    entity.MarkAsDeleted();
                    collection.Save();

                    prd = new Product();
                    prd.es.Connection.Name = "ForeignKeyTest";
                    Assert.IsFalse(prd.LoadByPrimaryKey(prdId));

                    OrderItemCollection oic = new OrderItemCollection();
                    oic.es.Connection.Name = "ForeignKeyTest";
                    oic.Query.Where(oic.Query.ProductID == prdId);
                    Assert.IsFalse(oic.Query.Load());
                }
            }
            finally
            {
                prd = new Product();
                prd.es.Connection.Name = "ForeignKeyTest";

                if (prd.LoadByPrimaryKey(prdId))
                {
                    prd.OrderItemCollectionByProductID.MarkAllAsDeleted();
                    prd.MarkAsDeleted();
                    prd.Save();
                }
            }
        }