Multi-level writing to Self-Joined table using a single UnitOfWork2.Commit (Adapter)

Posts   
 
    
greenstone
User
Posts: 132
Joined: 20-Jun-2007
# Posted on: 19-Feb-2009 14:17:33   

Hi,

I am creating a hierarchical tree using a self-joined table.

TreeTable (SQL Server): Id (Primary Key) (identity) ParentId (Foreign Key to Id) Field1 Field2 Field3

My tree table initially has data for only the top level (e.g. Id = 1, ParentId = null...) and I want to be able to add multiple lower levels in one UnitOfWork2.Commit.

TopLevel: Id = 1 OneLevelDownA: Id = not-yet-set TwoLevelsDownOfA_A: Id = not-yet-set TwoLevelsDownOfA_B: Id = not-yet-set OneLevelDownB: Id = not-yet-set TwoLevelsDownOfB_A: Id = not-yet-set TwoLevelsDownOfB_B: Id = not-yet-set

I traverse through the tree and add all entities to the UnitOfWork2:

for each treeEntity (recursively...though recursion is not shown here) { Business.Lower.EntityClasses.TreeEntity treeEntity1 = new Business.Lower.EntityClasses.TreeEntity();

treeEntity1.Field1 = "xx"; treeEntity1.Field2 = "yy"; treeEntity1.Field3 = "zz"; //treeEntity1.ParentId = ?; // HERE'S THE ISSUE/QUESTION

unitOfWork.AddForSave(treeEntity1); }

The issue is that I cannot set the TwoLevelsDownOfA_AEntity.ParentId directly because the OneLevelDownA.Id has not yet been set by the database (it's an identity field).

How can I set a relation between the parent entities and child entities, so the UnitOfWork2 will properly set the ParentIds?

Thanks!

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 19-Feb-2009 21:59:40   

Is the FK-PK relationship defined in the database ? and did the LLBLGen designer pick it up and create a relation between the entity and itself...?

If it did not, can you add the relation between the table and itself in the designer manually ?

Matt

greenstone
User
Posts: 132
Joined: 20-Jun-2007
# Posted on: 19-Feb-2009 22:25:41   

Hi Matt,

Thanks for the note.

Yes, the FK-PK relationship is defined in the database. I didn't spot where the FK-PK relationship was created in the designer build code. If that relationship does exist, would you know what it might be named/how I can find it?

Thanks!

greenstone
User
Posts: 132
Joined: 20-Jun-2007
# Posted on: 20-Feb-2009 04:06:19   

I found the llblgen generated relationship, and just add the child to the parent's collection...and the UnitOfWork2 seems to take care of all the ParentId's.

Thanks!