If I need to update or insert into multiple tables with one “action”, example a call to save the information, where there are multiple tables that incorporate ‘Information’.
For arguments sake, lets say we have the following tables:
Name
Address
Car
Job
and with each call to save information, each one of those tables is being inserted into.
Which is better:
- Get the data that must be written to Name table. Call InsertOnSubmit and call SubmitChanges
- Get the data that must be written to Address table. Call InsertOnSubmit and call SubmitChanges
- Get the data that must be written to Car table. Call InsertOnSubmit and call SubmitChanges
- Get the data that must be written to Job table. Call InsertOnSubmit and call SubmitChanges
or
- Get the data that must be written to Name table.
- Get the data that must be written to Address table.
- Get the data that must be written to Car table.
- Get the data that must be written to Job table.
- Call InsertOnSubmit, then call SubmitChanges.
or
- Get the data that must be written to Name table. Call InsertOnSubmit
- Get the data that must be written to Address table. Call InsertOnSubmit
- Get the data that must be written to Car table. Call InsertOnSubmit
- Get the data that must be written to Job table. Call InsertOnSubmit
- Call SubmitChanges
I have implemented the first option given and I am sure this is not the best way to go. I don’t know if I have problems with my network connection or whatever, but sometimes some of the tables are written to while others are not and this makes me sure I am not doing it the best way possible.
In some of the posts that are similar to this question, there are comments to do with Views and other options. I have no experience with this, so I need some guidance as to the best way to do this and which direction I should focus my attention on.
Prepare the data for the first table and execute an InsertOnSubmit for the table
then the same for the 2nd, 3rd, 4th table.
then execute the SubmitChanges.
In the database all rows wil be inserted (or updated or deleted if you include those kind of operations). If something goes wrong during the updates of the database ALL of the modifications will be removed (rolled back). So the integrety of the data is secure!
The data in the variables in your program will not reflect the database, if a roll back took place, you will need to access the data again.
The second variant does not always work well because it will only include the last table in the roll back. if insert of table 1 goes well, no 2 fails, nr 3 and 4 go well, then you will NOT notice the 2nd failure.