How to: Add, Modify, and Delete Entities (WCF Data Services)

 

With the WCF Data Services client libraries, you can create, update, and delete entity data in a data service by performing equivalent actions on objects in the DataServiceContext. For more information, see Updating the Data Service.

The example in this topic uses the Northwind sample data service and autogenerated client data service classes. This service and the client data classes are created when you complete the WCF Data Services quickstart.

The following example creates a new object instance and then calls the AddObject method on the DataServiceContext to create the item in the context. An HTTP POST message is sent to the data service when the SaveChanges method is called.

[!CODE [Astoria Northwind Client#AddProduct](../CodeSnippet/VS_Snippets_Misc/astoria northwind client#addproduct)]

The following example retrieves and modifies an existing object and then calls the UpdateObject method on the DataServiceContext to mark the item in the context as updated. An HTTP MERGE message is sent to the data service when the SaveChanges method is called.

[!CODE [Astoria Northwind Client#ModifyCustomer](../CodeSnippet/VS_Snippets_Misc/astoria northwind client#modifycustomer)]

The following example calls the DeleteObject method on the DataServiceContext to mark the item in the context as deleted. An HTTP DELETE message is sent to the data service when the SaveChanges method is called.

[!CODE [Astoria Northwind Client#DeleteProduct](../CodeSnippet/VS_Snippets_Misc/astoria northwind client#deleteproduct)]

The following example creates a new object instance and then calls the AddRelatedObject method on the DataServiceContext to create the item in the context along with the link to the related order. An HTTP POST message is sent to the data service when the SaveChanges method is called.

[!CODE [Astoria Northwind Client#AddOrderDetailToOrderAuto](../CodeSnippet/VS_Snippets_Misc/astoria northwind client#addorderdetailtoorderauto)]

WCF Data Services Client Library
How to: Attach an Existing Entity to the DataServiceContext
How to: Define Entity Relationships
Batching Operations

Show: