private async Task ExecuteChangeSet(
            ChangeSetRequestItem changeSet,
            IList<ODataBatchResponseItem> responses,
            CancellationToken cancellation)
        {
            ChangeSetResponseItem changeSetResponse;

            // Create a new ShoppingContext instance, associate it with each of the requests, start a new
            // transaction, execute the changeset and then commit or rollback the transaction depending on
            // whether the responses were all successful or not.
            using (ShoppingContext context = new ShoppingContext())
            {
                foreach (HttpRequestMessage request in changeSet.Requests)
                {
                    request.SetContext(context);
                }

                using (DbContextTransaction transaction = context.Database.BeginTransaction())
                {
                    changeSetResponse = (ChangeSetResponseItem)await changeSet.SendRequestAsync(Invoker, cancellation);
                    responses.Add(changeSetResponse);

                    if (changeSetResponse.Responses.All(r => r.IsSuccessStatusCode))
                    {
                        transaction.Commit();
                    }
                    else
                    {
                        transaction.Rollback();
                    }
                }
            }
        }
        public static void Register(HttpConfiguration config)
        {
            // Read the model directly from entity framework.
            IEdmModel model;
            using (ShoppingContext context = new ShoppingContext())
            {
                model = context.GetEdmModel();
            }

            // Create our batch handler and associate it with the OData service.
            ODataBatchHandler batchHandler = new EntityFrameworkBatchHandler(GlobalConfiguration.DefaultServer);
            config.Routes.MapODataServiceRoute("odata", "odata", model, batchHandler);
        }
        /// <summary>
        /// Retrieves a <see cref="ShoppingContext"/> instance associated with the current request. If no instance is
        /// associated it will create a new one, associate it with the current request and register it for disposal at
        /// the end of the request.
        /// </summary>
        /// <param name="request">The request from which to retrieve the <see cref="CustomerContext"/> instance.</param>
        /// <returns>A <see cref="CustomerContext"/> instance associated with this request.</returns>
        public static ShoppingContext GetContext(this HttpRequestMessage request)
        {
            object customersContext;
            if (request.Properties.TryGetValue(DB_Context, out customersContext))
            {
                return (ShoppingContext)customersContext;
            }
            else
            {
                ShoppingContext context = new ShoppingContext();
                SetContext(request, context);

                request.RegisterForDispose(context);
                return context;
            }
        }
 /// <summary>
 /// Associates a given instance of a DbContext to the current request.
 /// </summary>
 /// <param name="request">The request to will the DbContext instance will be associated.</param>
 /// <param name="context">The DbContext instance to associate with the request.</param>
 public static void SetContext(
     this HttpRequestMessage request,
     ShoppingContext context)
 {
     request.Properties[DB_Context] = context;
 }