silverlightwcfwcf-data-servicescascadeastoria

Cascade deletion in WCF Data Services


Is it possible to delete persistent objects graph by the single command using WCF Data Services in Silverlight? I've started with this walkthrough and added code for deletion of selected order (which contains details):

private void deleteOrder_Click(object sender, RoutedEventArgs e)
{
    Order deletedOrder = (Order)ordersGrid.SelectedItem;

    // Delete the selected order.
    svcContext.DeleteObject(deletedOrder);

    // Remove the deleted order from the binding collection.
    ordersGrid.SelectedIndex = ordersGrid.SelectedIndex == 0 ? 1 : ordersGrid.SelectedIndex - 1;
    ordersBindingCollection.Remove(deletedOrder);
}

When I save changes back to server, delete request is issued:

--batch_2009b119-0747-4019-8974-8ea7dd29963a
Content-Type: multipart/mixed; boundary=changeset_b451aecf-b66d-4f0f-8e6d-8a067646b350

--changeset_b451aecf-b66d-4f0f-8e6d-8a067646b350
Content-Type: application/http
Content-Transfer-Encoding: binary

DELETE http://localhost:55378/PurchasesService.svc/Orders(1) HTTP/1.1
Content-ID: 90

--changeset_b451aecf-b66d-4f0f-8e6d-8a067646b350--
--batch_2009b119-0747-4019-8974-8ea7dd29963a--

But it fails on the database end with the foreign key constraint violation:

--batchresponse_b6c22c24-17ec-409b-ba91-91784116927d
Content-Type: multipart/mixed; boundary=changesetresponse_41e6e873-a5dd-446d-bda0-99405ba92a63

--changesetresponse_41e6e873-a5dd-446d-bda0-99405ba92a63
Content-Type: application/http
Content-Transfer-Encoding: binary

HTTP/1.1 500 Internal Server Error
Content-ID: 90
Cache-Control: no-cache
DataServiceVersion: 1.0;
Content-Type: application/xml

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
  <code></code>
  <message xml:lang="uk-UA">An error occurred while processing this request.</message>
</error>
--changesetresponse_41e6e873-a5dd-446d-bda0-99405ba92a63--
--batchresponse_b6c22c24-17ec-409b-ba91-91784116927d--

I've also tried to set relation's OnDelete property to Cascade in .edmx

Is it possible to make such deletions? Who should take care of deleting referenced objects: client or server? And how many delete requests should be sent to the server: one or N+1 (where N is number of details of given order). Maybe I'm missing some key point. Are there any WCF Data Services tutorials describing deletion of object graphs?


Solution

  • YAY! I figured it! =D Ive been at the same thing for hours today!

    So I didn't set Cascade in the edmx.

    You set it in the database

    In SQL Server Management Studio:

    1. In a database diagram select the relationship you want to cascade delete
    2. Open properties
    3. Expand Insert And Update
    4. Set the Delete rule to Cascade.

    Reload/Update your entity framework model. When you do this, visual studio will update the CSDL/SSDL (Whatever?).

    Reference from MSDN:
    "When you want to automatically delete all of the child records of a parent when the parent record is deleted you can specify the cascade delete rule. It is strongly recommended that you specify the cascade delete rules in both the conceptual model and the database."