With Linq To Sql – what is the best way to update the value of one table in a database when a value changes in a different table?
For example in TableA, there is a column called DateModified. TableA has an association to TableB. Now I want to set the value of the DateModified field to the current date every time the record changes in tableA AND everytime a child record is created/updated/deleted in TableB.
How can this be achieved?
If the change was just in the one record, you can use either the
On*Changedpartial methods, or you can overrideSubmitChangeson the data-context, callGetChangeSet, and apply the changes just before they are updated. The latter (data-context) approach is useful for catching a broad spectrum of changes, where-as theOn*Changedapproach is useful for logic specific to individual properties.However, in the case presented (non-homogeneous updates), I expect a database trigger may be more appropriate.