Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

6.6. Updating data

If we were limited to fetching data, the functionality would be no more than a reporting tool. Luckily, updating the data is just as simple as fetching it. As long as we have a persistent DataContext object, we can make additions, alterations, and deletions using standard methods on the table objects. The DataContext keeps track of the changes and handles updating the database with a single method call.

To begin, let’s look at an example that updates the price of our most expensive books so we can offer a discount on them (see listing 6.28). In this case, we fetch only the books that are “expensive”—where the price is greater than $30—and then iterate over them, reducing the price of each by $5. Finally, we persist the changes to our database by calling the SubmitChanges method on our DataContext object.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint