Hi,
A rule of thumb is that if table1’s id column is the foreign key of table2, you should definitely insert a new record into table1 first and then insert another record to the table2 with respect to the newly inserted record in table1.
Your own solution to insert a new record to both tables with the id column’s AutoGenerate property is set to false is correct.
If the id column’s AutoGenerate property is set to true, then you can insert a new record in the following way.
· Insert a new record into Table1. The insert command doesn’t need to specify the value of the ID column since it is auto generated by the SQL Server.
· Call the SCOPE_IDENTITY stored procedure to retrieve the ID value of the latest inserted record.
· Once you get the ID value, use it to compose another SQL insert command which insert a record into the Table2.
Here’s an article about Retrieving Identity or Autonumber Values (ADO.NET)
Regards,
Jacob