adding entries into multiple tables at a time

Posts   
 
    
kvelidi
User
Posts: 3
Joined: 19-Nov-2007
# Posted on: 19-Nov-2007 23:49:53   

Hi,

I have 2 tables

Log( LogID(PK), Name). LogID is autogenerated value by the database.I just need to insert Name .

The other Table is Station(StationID, LogID). LogID in "Station" references LogID in "Log" Table.

When I insert a Name in Log Table, I need to insert the generated LogID into Station table. How can I do this.

Thanks

krishna.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 20-Nov-2007 03:56:29   

Hi krishna,

You can do something like this simple_smile

LogEntity newLog = new LogEntity();
newLog.Name = "XXX";

StationEntity newStation = new StationEntity();
newStation.StationID = "123";

// this allows the PK-FK sync, 
//so when you save newLog, the just autogenerated LogId will be asigned to newStation.LogId 
newLog.Stations.Add(newStation);

// if you are using Adapter templateSet
using (DataAccessAdapter adpater = new DataAccessAdapter())
{
     // will save recursive, and will refetch the newest values
     adapter.SaveEntity(newLog, true, true);
}

// if you are using SelfServicing templateSet
// will save recursive, and will refetch the newest values (that is the nature of SelfServicing)
newLog.Save(true);
David Elizondo | LLBLGen Support Team