Load relation on insert via unique field

Posts   
 
    
Roman
User
Posts: 23
Joined: 19-May-2006
# Posted on: 01-Jul-2006 00:09:46   

Hi, I have the following scenario. There is a MainTable, along with RelationTable1, RelationTable2, RelationTable3, etc. On MainTable, there is a relation with those other tables via foreign keys, in other words there are fields like RelationTable1_ID, RelationTable2_ID, etc. Additionally, each RelationTable has a unique Code field (these are simple read-only lookup tables).

When creating a new MainTable entity, I need to populate those relation fields. One way is via the FK, where I already know the key value and just set the RelationTableX_ID field. Unfortunately, the application is not aware of specific keys (they are just ints), but does know specific codes. So what I could do is perform a GetScalar on each of those tables, retrieve the PK for the specified code and put that into the new MainTable entity.

That of course results in many roundtrips to the database. Is there a way to do this in one step? Something like..

MainTableEntity.RelationTable1 = new RelationTable1Entity(); MainTableEntity.RelationTable1.Code = SOME_CODE;

and then somehow there will be a fetch using the unique key DURING the insert and the RelationTable1_ID will be populated automagically?

The only way to do a single roundtrip that I see is via a stored proc. Is there a way to make the number of roundtrips less than # of FKs + 1 (one select to get the PK_ID for each RelationTable + 1 save for the MainTable entity)?

Thanks!

P.S. I'm using adapter.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 01-Jul-2006 08:49:50   

If they're lookup tables, you could also cache them, is that a possibility? Even if you'd use a proc, it would take a lot of database activity I think to get the ID's in 1 set.

Frans Bouma | Lead developer LLBLGen Pro
Roman
User
Posts: 23
Joined: 19-May-2006
# Posted on: 05-Jul-2006 18:37:27   

Yep, that's what I'll do. Thanks!