By default,
LINQ to SQL keeps track of any changes made to these objects via the LINQ to SQL DataContext.
It??™s important to remember that the changes are not tracked on the database side. They are tracked on the
client side, and no database resources are used while changes are tracked.
In Chapter 12 you saw that you could work with read-only results by setting the ObjectTrackingEnabled
property of the DataContext to false. This property also enables you to track changes made to objects and
to submit those changes to the underlying database.
Tracking changes is enabled by default, so changes made to objects within your object model are automatically
tracked. When the application is ready to send the changes back to the database, a simple call
to the DataContext SubmitChanges() method needs to take place. That method instructs LINQ to SQL
to execute the appropriate SQL code to make the necessary changes to update the database.
For example, the following code creates a new contact object, saves it to the database, updates an existing
contact object, and then saves the changes back to the database.
Pages:
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416