views:

976

answers:

2

We're trying to play around with RIA Services. I can't seem to figure out how to delete a record. Here's the code I'm trying to use.

   SomeDomainContext _SomeDomainContext = (SomeDomainContext)(productDataSource.DomainContext);
    Product luckyProduct = (Product)(TheDataGrid.SelectedItem);

    _SomeDomainContext.Products.Remove(luckyProduct);

    productDataSource.SubmitChanges();

The removing the object from the Entity part works fine, but it doesn't seem to do anything to the DB. Am I using the objects like I'm supposed to, or is there a different way of saving things?

+1  A: 

The error system is a little finicky. Try this o get the error if there is one and that will give you an idea. My problem was dependencies to other tables needing deletion first before the object could be. Ex: Tasks deleted before deleting the Ticket.

System.Windows.Ria.Data.SubmitOperation op = productDataSource.SubmitChanges();
op.Completed += new EventHandler(op_Completed);

void TicketsLoaded_Completed(object sender, EventArgs e) {
   System.Windows.Ria.Data.SubmitOperation op = (System.Windows.Ria.Data.SubmitOperation)sender;
   if (op.Error != null) {
      ErrorWindow view = new ErrorWindow(op.Error);
      view.Show();
   }
}
Tacoman667
How do you assign SubmitChanges to a LoadOperation? It returns a void. I think that you're right, and it would be nice to get the error message, but I don't see how to use your code. In other news, I think that our problem was dependencies (as we connect to a simpler table and now can delete), so if this isn't the correct answer, it at least led me to a solution. Thanks for your help.
thepaulpage
Ack! I put LoadOperation and I certainly meant SubmitOperation. I will change that in the code above.
Tacoman667
+1  A: 

In the code snippet above, I'd suggest using the callback parameter rather than an event handler.

productsDataSource.SubmitChanges(delegate(SubmitOperation operation) {
    if (operation.HasError) {
        MessageBox.Show(operation.Error.Message);
    }
}, null);

The callback model is designed for the caller of Load/SubmitChanges, while the event is designed for other code that gets a reference to a LoadOperation/SubmitOperation.

Hope that helps...

NikhilK