Copy Records With Table Hierarchy

Posts   
 
    
Dan1
User
Posts: 41
Joined: 20-Sep-2007
# Posted on: 25-Apr-2008 16:10:53   

Is there any way with LLBLGen Pro generated code (SS,2.5) to copy records and the child path to a new set of records?

Currently I do this in a stored procedure using complex joins. Take a structure like this as follows:

tblParent (uses identity primary key column) -->tblChildren (uses identity primary key column) -->tblChildrensChildren (uses identity primary key column)

The only real complex part of this scenario is that when you copy the record in tblParent to a new record you must copy all of the children but keep the foreign key of the new parent and disregard the old foreign key, and then you must do the same thing for all of the ChildrensChildren. It's involved me having to write stored procedures with fairly complex joins and FETCH statements. New primary keys are generated for new (copied) records so after a record is inserted, the new primary key must be retrieved and is used as te foreign key in the child table.

I'm not asking if I can use the stored procedure in LLBGen Pro because I know I can, I am wondering if I could disregard the stored procedure and do this failr simple with LLBLGen Pro? Is there something in the object model I am missing that could simplify this or are we would I have to do a similar thing with LLBLGen Pro that I am with the stored procedures but just with using .NET code (Loops, entity loads, updates, etc).

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 26-Apr-2008 04:39:18   

Could you please indicate what are you trying to achieve?

(Given a Stored Procedure, fetch the results and fill entity objects hierarchically??)

David Elizondo | LLBLGen Support Team
Dan1
User
Posts: 41
Joined: 20-Sep-2007
# Posted on: 26-Apr-2008 05:31:53   

The app allows the user to copy the current record (and all child records in child tables) to a new set of records to use as a template for a new project while keeping the old records in tact and the new records are exact duplicates of the ones they were copied from except all of the primary keys and foreign keys would be different.

How do you recommend I do this with LLBLGen Pro? Currently I am using SQL Server Stored Procedures, but I am wondering if LLBLGen Pro would be simpler or if there was something built into the object model that would facilitate this.

Posts: 1255
Joined: 10-Mar-2006
# Posted on: 26-Apr-2008 05:35:46   

If you already have this working in a stored proc, why would you want to change that?

Plus, to do it in LLBLGen entities, you would have to fetch all the data, only to send it all back for saving. Stored procedure would be much more efficent.

However, if you insisted in doing it in LLBLGen, I guess you could:

  • loop through all entities and mark all fields a being modified and mark all entities as new.
  • save recursively

Again, highly recommend leaving well enough alone and keeping the stored proc running! simple_smile

Dan1
User
Posts: 41
Joined: 20-Sep-2007
# Posted on: 26-Apr-2008 05:46:45   

If that's the only way (as expected) then I am fine keeping it as is. I've just added a couple of tables to the application that I haven't written the copy routines for yet so I figured now is the time to re-evaluate it. Advantage of .NET code is simpler to debug (Grrr.. if we had moved our DB to SQL Server 2005 by now we could even investigate a .NET Code Procedure on the server wink )

Unless anyone else has any different insight on this I'll consider this the end of the thought for me to use LLBLGen Pro for this task.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 26-Apr-2008 06:37:42   
David Elizondo | LLBLGen Support Team
Posts: 1255
Joined: 10-Mar-2006
# Posted on: 26-Apr-2008 13:44:17   

Good link - that would be how you would implement what I suggested. Although wouldn't it be a problem to have the pkey already set? Might need to empty that (since it is identity) - dunno LLBLGen pro layer might ignore it on IsNew anyway...

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 27-Apr-2008 20:45:33   

If the PK is autoincrement or has a sequential routine the PK-reset is no needed, though if it isn't, the PK should be reseted as you pointed.

David Elizondo | LLBLGen Support Team