How do I set something to remove related data on save?

Posts   
 
    
JMitchell avatar
JMitchell
User
Posts: 128
Joined: 01-Sep-2006
# Posted on: 04-Nov-2010 12:53:27   

A summary of the relevant structure of my database is: **Booking **>> **RoomDetail **>> NightDetail

If you change the number of nights in the RoomDetail, the number of NightDetail entities will change.

At the moment I have custom entity code set up to override OnFieldValueChanged - when the number of nights changes, it calls a function which adds or deletes NightDetails entities.

This works fairly well unless people modify the number of nights and then exit without saving as in that instance it has deleted the old NightDetail entities that are still required.

If I just remove the entities, when it is saved it doesn't delete the removed items and they are still in the database and thus still linked next time you load that RoomDetail.

Would my best bet be to override the save function? - Save(ByVal updateRestriction As SD.LLBLGen.Pro.ORMSupportClasses.IPredicate, ByVal recurse As Boolean)

If I do that, I could load the data from the database before the save, delete any items which are no longer related and then continue with the save.

Two related questions really:

**1. Is there a way to remove a child entity's relation to the parent entity and only have it deleted from the database when the parent entity is saved? (sounds unlikely)

  1. If not, could overriding the save function identified above work and does it sound like the best way to do it?**

Thanks

James

(VB.NET in VS2008, LLBLGen Pro 2.6 self servicing)

JMitchell avatar
JMitchell
User
Posts: 128
Joined: 01-Sep-2006
# Posted on: 04-Nov-2010 16:17:34   

Overriding Save doesn't seem to have been the right thing to do.

I've tried overriding OnSave and OnSaveComplete and they are being triggered but it seems there is some kind of database lock whereby I can't load the existing items. The last lines of the following options both timeout from within subs overriding OnSave and OnSaveComplete in HotelRoomDetailEntity:

Dim NewRoom As New HotelRoomDetailEntity(Me.BookingDetailId)
Dim OldNightCount As Integer = NewRoom.BookingDetail.NightRoomRate.Count
Dim OldNightRateCollection As New NightRoomRateCollection
Dim filter As IPredicateExpression = New PredicateExpression(NightRoomRateFields.BookingDetailId = Me.BookingDetailId)
OldNightRateCollection.GetMulti(filter)
Dim OldNightCount As Integer = OldNightRateCollection.Count

Edit 04/11 16:41

I've found some help topics and forum posts regarding UnitOfWork and RemovedEntitiesTracker but I can't see where to fit it into the generated code.

JMitchell avatar
JMitchell
User
Posts: 128
Joined: 01-Sep-2006
# Posted on: 04-Nov-2010 18:01:58   

I've got it sorted now.

It's probably partly because of my strange database structure!

In the PrivateMembers area of my BookingDetail entity I've got the code:

Private tracker As New NightRoomRateCollection()

In the CustomEntityCode user code area I've got:

Protected Overrides Sub OnFetchComplete()
    Me.NightRoomRate.RemovedEntitiesTracker = tracker
    MyBase.OnFetchComplete()
End Sub

In the same section of my RoomDetail area (where the change would occur) I've got:

Protected Overrides Sub OnSave()
    Dim uow As New UnitOfWork
    uow.AddCollectionForDelete(Me.BookingDetail.NightRoomRate.RemovedEntitiesTracker)
    uow.Commit(New Transaction(IsolationLevel.ReadCommitted, "UOW"), True)
    MyBase.OnSaveComplete()
End Sub