More Saving Options

If the provider implements IUpdatable, ADO.NET Data Services also supports adding and deleting single objects or object graphs. You add an object by creating an instance of the entity and using the context’s AddTo[EntitySetName] method for the corresponding entity set. Then you can execute the context’s BeginSaveChanges method to apply the changes and add the new entities to the underlying data source through ADO.NET Data Services. You can handle deletes by passing the object to delete to the context’s DeleteObject method. Then you can execute the context’s BeginSaveChanges method to apply the deletions through ADO.NET Data Services.

Inserts and Object Graphs

The DeepInsert user control in the sample code in the EFDataServices solution creates a series of related objects, links them together in an object graph, and adds them to the context. Then the object graph is saved via the data service. The DeepInsertTest method shown in Example 11-26 creates two Product instances and creates a new Category and Supplier instance for the new Product instances. The new Category and Supplier instances are linked to the new products using the context’s SetLink method. Finally, the object graph is passed to the data service through the context’s BeginSaveChanges method.

Example 11-26. Deep insert of an object graph

C# private void DeepInsertTest() { Supplier sockSupplier = new Supplier { Address = "1 Lois Lane", City = "Somewhere", CompanyName = "Socks R Us", ContactName = "Ella", ...

Get Data-Driven Services with Silverlight 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.