Condividi tramite


Batching Operations (ADO.NET Data Services)

In ADO.NET Data Services, each operation that uses the DataServiceContext, such as executing a query or saving changes, results in a separate request being sent to the data service. In order to maintain a logical scope for sets of operations, you can explicitly define operational batches. This ensures that all operations in the batch are sent to the data service in a single HTTP request, enables the server to process the operations atomically, and reduces the number of round trips to the data service. For more information, see Batch Requests (ADO.NET Data Services).

Batching Query Operations

To execute multiple queries in a single batch, you must create each query in the batch as a separate instance of the DataServiceRequest class. When you create a query request in this manner, the query itself is defined as a URI, and it follows the rules for addressing resources. For more information, see Addressing Resources (ADO.NET Data Services). The batched query requests are sent to the data service when the ExecuteBatch(array<DataServiceRequest[]) method is called that contains the query request objects. This method returns a DataServiceResponse object, which is a collection of QueryOperationResponse objects that represent responses to individual queries in the batch, each of which contains either a collection of objects returned by the query or error information. When any single query operation in the batch fails, error information is returned in the QueryOperationResponse object for the operation that failed and the remaining operations are still executed. For more information, see How to: Execute Queries in a Batch (ADO.NET Data Services).

Batched queries can also be executed asynchronously. For more information, see Asynchronous Operations (ADO.NET Data Services).

Batching the SaveChanges Operation

When you call the SaveChanges() method, all changes that the context tracks are translated into REST-based operations that are sent to the service as HTTP requests. By default, these changes are not sent in a single request message. To require that all changes be sent in a single batch, you must call the SaveChanges(SaveChangesOptions) method and include a value of Batch() in the SaveChangesOptions enumeration that you supply to the method.

You can also asynchronously save batched changes. For more information, see Asynchronous Operations (ADO.NET Data Services).

See Also

Other Resources

Using a Data Service in a .NET Framework Application (ADO.NET Data Services)