UnitOfWork, Transactions and identity values

Posts   
 
    
omar avatar
omar
User
Posts: 569
Joined: 15-Oct-2004
# Posted on: 21-Feb-2006 06:14:20   

I have standarized all my LLBL data-access code to use the UnitOfWork but now I am facing the following issue;

1- entity (HRAnnualLeave) has an identity field (cntId) 3- the same uow object used to save the (HRAnnualLeave) entity is also used to save a (HRMonthlyAddition) entity 4- I want the (HRAnnualLeave) entity's identity value to be saved in a field in the (HRMonthlyAddition) entity

the code goes as follows:


      'Me referes to the current instance of the HRAnnualLeave
      uow.AddForSave(Me, refetch:=True)

      Dim ma As HRMonthlyAddition = New HRMonthlyAddition
      With ma
         .StrEmployeeId = Me.StrEmployeeId
         .IntLink = Me.CntId  'should get Me's identity value but gets zero!!
      End With
      uow.AddForSave(ma)

      uow.Commit(adapter, True)

Now, I do want both save operations to execute in the context of the same transaction (both must pass or fail together) but I also want the identity value of (Me.cntId) to store in (ma.IntLink ) object!!

I guess I can commit the (Me) object to save and then get its identity value to store in the (ma) object but then I lose saving both entites in the same transactionconfused

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 21-Feb-2006 06:41:30   

Do the HRAnnualLeave & HRMonthlyAddition have a PK-FK relation through the cntId ?!

If so, you can add the HRAnnualLeave to the related field inside HRMonthlyAddition and add the later to the unit of work, and the PK-FK will be synchronized

omar avatar
omar
User
Posts: 569
Joined: 15-Oct-2004
# Posted on: 21-Feb-2006 06:57:34   

Walaa wrote:

Do the HRAnnualLeave & HRMonthlyAddition have a PK-FK relation through the cntId ?!

If so, you can add the HRAnnualLeave to the related field inside HRMonthlyAddition and add the later to the unit of work, and the PK-FK will be synchronized

No... they don't have any direct relation (although both are related to HREmployee HREmployee --one-to-many--> HRAnnualLeave HREmployee --one-to-many--> HRMonthlyAddition)

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 21-Feb-2006 07:19:31   

I think you may use LBLGen Pro to define that missing relation (an Identity Column can have a unique constraint, it won't make a difference).

Otherwise I see no way than doing it manually, save the first Entity and refetch it, then use the Identity value to fill the other Entity and Save it. And you may Delete the first Entity if the Other wasn't saved correctly. (Sure you will miss having the Transaction, sorry) disappointed

omar avatar
omar
User
Posts: 569
Joined: 15-Oct-2004
# Posted on: 21-Feb-2006 08:36:31   

Walaa wrote:

I think you may use LBLGen Pro to define that missing relation (an Identity Column can have a unique constraint, it won't make a difference).

Otherwise I see no way than doing it manually, save the first Entity and refetch it, then use the Identity value to fill the other Entity and Save it. And you may Delete the first Entity if the Other wasn't saved correctly. (Sure you will miss having the Transaction, sorry) disappointed

The problem with this relation is that its dependant on another column in HRMonthlyAddtion as follows: HRAnnualLeave.cntId --1-to-1--> HRMonthlyAddition.intLink + strMonthlyAdditionType='Annual Leave' this meens that HRMonthlyAddition.intLink=5 does NOT relate to (HRAnnualLeave.cntId=5) BUT HRMonthlyAddition.intLink=5 relates to (HRAnnualLeave.cntId=5 And HRAnnualLeave.strMonthlyAdditionType='Annual Leave')

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 21-Feb-2006 09:37:34   

You could pass in a derived class of DataAccessAdapter in which you've overriden OnSaveEntityComplete() and in the override you check if the entity passed in is the one you want to ID of,then add it to the other entity. Really dirty, but doable.

At the moment there's no other way to intercept save activity during a transaction than OnSaveEntity() and OnSaveEntityComplete.

Frans Bouma | Lead developer LLBLGen Pro
Angus
User
Posts: 44
Joined: 08-Jul-2005
# Posted on: 25-Feb-2006 23:16:59   

I know that this might not be of much use on this project, but I switched to using GUID's for my PK columns. It has helped avoid this problem, as I am able to create the PK anywhere in code using

Guid pk = Guid.NewGuid();

Switching over has saved me quite a few headaches with getthing realations to work correctly before being inserted into the DB.

Chris.