locked
Client model of data does not seem to want to update after a delete RRS feed

  • Question

  • I'm a little new to WCF OData clients, so I may be missing something, but I cannot seem to get the client of my OData service to reflect the fact that data it has deleted has in fact been deleted, even though the server has processed the request and no longer represents the data as being there.

    My data model looks something like this:

    class Graph {
      ...
      List<Statement> Statements;
    }
    
    class Statement {
      ...
    }
    

    On the test client I'm writing, the generated code represents Graph.Statements as a DataServiceCollection<Statement> instead of a List<Statement>.  I then have some unit test code that tries to add and delete a statement from a graph, then verifies that it's been deleted:

    public void TestRemoveStatement()
    {
      Graph graph = CreateNewGraph();
      AddRandomStatementToGraph(graph);
      // Model is the DataServiceContext derived object managing the classes above.
      Model.SaveChanges();
    
      Model.LoadProperty(graph, "Statements");
      int count = graph.Statements.Count;  // There is one statement here.
    
      Statement s = graph.Statements[0];
      Model.DeleteLink(graph, "Statements", s);
      Model.DeleteObject(s);
      Model.SaveChanges();
    
      // The service this client is connecting to at this point receives calls to delete the statement, and does so.
    
      Model.UpdateObject(graph);
      Model.LoadProperty(graph, "Statements");
      int newCount = graph.Statements.Count;  // Expected this to be one less than the count above, but it isn't.
    }

    I can watch the client requesting an update of the appropriate URI when the last LoadProperty call is made and can see that the data returned contains no statements, as expected.  However, when I look at the collection of statements for the graph in the client, it still contains the old statement, undeleted.  How do I get the client's model of the data to update itself properly?  I assume I've missed a call somewhere....

    For completeness, I'm using version 5.6.0 of the client assemblies (Microsoft.Data.Edm, Microsoft.Data.OData, etc.) and .NET 4.5 in Visual Studio 2012.

    Friday, December 5, 2014 12:09 AM

Answers

  • Hello Charles,

    I assume that you are using the WCF Data Service project since you posted this issue to here and according to your description, I create a test demo which reproduces this issue you describes.

    After searching for a while, I find the DeleteLink method only delete the entity set represents the link relationship, for the navigation property, it still stays in the local memory. A workaround is to use the Rmeove() instead as below:

    static void Main(string[] args)
    
            {
    
                WCFDBEntities Model = new ServiceReference1.WCFDBEntities(new Uri("http://localhost:16668/WcfDataService1.svc/"));
    
                Order order = new ServiceReference1.Order() { OrderID = 1, OrderName = "1" };
    
                OrderDetail od = new OrderDetail() { OrderDetailID = 1, OrderDetailName = "1", OrderID = 1 };
    
                // Model is the DataServiceContext derived object managing the classes above.
    
                Model.AddToOrders(order);
    
                Model.AddToOrderDetails(od);
    
                Model.SaveChanges();
    
    
                Model.LoadProperty(order, "OrderDetails");
    
                int count = order.OrderDetails.Count;  // There is one statement here.
    
    
                OrderDetail odnew = order.OrderDetails[0];
    
                //Model.DeleteLink(order, "OrderDetails", odnew);
    
                order.OrderDetails.Remove(odnew);
    
                Model.DeleteObject(odnew);
    
                Model.SaveChanges();
    
    
                // The service this client is connecting to at this point receives calls to delete the statement, and does so.
    
    
                Model.UpdateObject(order);
    
                Model.LoadProperty(order, "OrderDetails");
    
                int newCount = order.OrderDetails.Count;  // Expected this to be one less than the count above, but it isn't.
    
    
            }
    

    After using the Remove, it also deletes the navigation property and the newCount is 0.

    Please have a try and if this does not work for you, please feel free let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, December 5, 2014 3:00 AM
    Moderator

All replies

  • Hello Charles,

    I assume that you are using the WCF Data Service project since you posted this issue to here and according to your description, I create a test demo which reproduces this issue you describes.

    After searching for a while, I find the DeleteLink method only delete the entity set represents the link relationship, for the navigation property, it still stays in the local memory. A workaround is to use the Rmeove() instead as below:

    static void Main(string[] args)
    
            {
    
                WCFDBEntities Model = new ServiceReference1.WCFDBEntities(new Uri("http://localhost:16668/WcfDataService1.svc/"));
    
                Order order = new ServiceReference1.Order() { OrderID = 1, OrderName = "1" };
    
                OrderDetail od = new OrderDetail() { OrderDetailID = 1, OrderDetailName = "1", OrderID = 1 };
    
                // Model is the DataServiceContext derived object managing the classes above.
    
                Model.AddToOrders(order);
    
                Model.AddToOrderDetails(od);
    
                Model.SaveChanges();
    
    
                Model.LoadProperty(order, "OrderDetails");
    
                int count = order.OrderDetails.Count;  // There is one statement here.
    
    
                OrderDetail odnew = order.OrderDetails[0];
    
                //Model.DeleteLink(order, "OrderDetails", odnew);
    
                order.OrderDetails.Remove(odnew);
    
                Model.DeleteObject(odnew);
    
                Model.SaveChanges();
    
    
                // The service this client is connecting to at this point receives calls to delete the statement, and does so.
    
    
                Model.UpdateObject(order);
    
                Model.LoadProperty(order, "OrderDetails");
    
                int newCount = order.OrderDetails.Count;  // Expected this to be one less than the count above, but it isn't.
    
    
            }
    

    After using the Remove, it also deletes the navigation property and the newCount is 0.

    Please have a try and if this does not work for you, please feel free let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, December 5, 2014 3:00 AM
    Moderator
  • That does seem to be the trick I needed to get the client copy to reflect things properly, thank you!
    Friday, December 5, 2014 5:51 PM