Using ActiveRecord / NHibernate, can I uninstall and update without flashing?

I have the following Unit Test method:

void TestOrderItemDelete()
{
    using (new SessionScope())
    {
        var order = Order.FindById(1234);
        var originalItemCount = order.OrderItems.Count;
        Assert.IsTrue(originalCount > 0);
        var itemToDelete = order.OrderItems[0];
        itemToDelete.DeleteAndFlush(); // itemToDelete.Delete();
        order.Refresh();
        Assert.AreEqual(originalCount - 1, order.OrderItems.Count);
    }
}

      

As you can see from the comment after the DeleteAndFlush command, I had to change it from a simple Delete to pass the Unit Test. Why is this? The same does not apply to my other Unit Test for adding OrderItem. This works great:

void TestOrderItemAdd()
{
    using (new SessionScope())
    {
        var order = Order.FindById(1234);
        var originalItemCount = order.OrderItems.Count;
        var itemToAdd = new OrderItem();
        itemToAdd.Order = order;
        itemToAdd.Create(); // Notice, this is not CreateAndFlush
        order.Refresh();
        Assert.AreEqual(originalCount + 1, order.OrderItems.Count);
    }
}

      

This all happened when I started using Lazy Instantiation to map Order.OrderItems relationships and had to add a use block (new SessionScope) around the test.

Any ideas?

0


source to share


1 answer


This is hard to fix without knowing the content of your mappings, but one possibility is that you have an ID property of the OrderItem object mapped using an identity field (or sequence, etc.) in the DB. If so, NHibernate has to make a trip to the database to generate the ID field, so the OrderItem is inserted immediately. This does not apply to delete, so the delete SQL statement is not executed until the session ends.



+1


source







All Articles