locked
LoadProperty is not refreshing the property when one of the object in the property collection is deleted. RRS feed

  • Question

  • LoadProperty is not refreshing the property when one of the object in the property collection is deleted.

    Sample code :

       WalmartContainer context = new WalmartContainer(new Uri("http://localhost:58570/WcfDataService1.svc/"));
    
       
    
       Customer customer = new Customer() { Name = "customerName" };
    
       Items item1 = new Items() { Name = "item1" };
    
       Items item2 = new Items() { Name = "item2" };
    
       
    
       context.AddToCustomers(customer);
    
       context.SaveChanges();
    
    
    
       context.AddToItems(item1);
    
       context.SetLink(item1, "Customer", customer);
    
       context.AddLink(customer, "Items", item1);
    
       context.SaveChanges();
    
    
    
       context.AddToItems(item2);
    
       context.SetLink(item2, "Customer", customer);
    
       context.AddLink(customer, "Items", item2);
    
       context.SaveChanges();
    
    
    
       foreach (Customer c in context.Customers)
    
       {
    
        Console.WriteLine(c.Name);
    
        context.LoadProperty(c, "Items"); // Both item1 and item2 are being loaded.
    
        foreach (Items i in c.Items)
    
        {
    
         Console.WriteLine(i.Name);
    
        }
    
       }
    
       Console.ReadKey();
    
    
    
       context.DeleteObject(item2);
    
       context.SaveChanges();
    
    
    
       foreach (Customer c in context.Customers)
    
       {
    
        Console.WriteLine(c.Name);
    
        context.LoadProperty(c, "Items"); // Even item2 is appearing as part of c.Items
    
        foreach (Items i in c.Items)
    
        {
    
         Console.WriteLine(i.Name);
    
        }
    
       }
    
       Console.ReadKey();
    
    

    When LoadProperty(c, "Items") is called for the second time, even the deleted object is appearing as part of c.Items collection. Doing a LoadProperty(c, "Items") after Nullifying c.Items works but I would be loosing those items that I have added to c.Items using c.Items.Add() and not yet saved.

    How can I make sure that the property which is a collection is properly refreshed/merged when a couple of items are deleted or added. (Please dont tell me to refer to DataServiceContext.Entities list and do the merging manually :) )

    Is this issue by design or a defect which is going to be fixed later or am I missing something here?

    Thanks,

    Kowshik Palivela

     


    • Edited by kowshik - MSFT Tuesday, April 5, 2011 12:18 PM Updated the title to be apt
    Tuesday, April 5, 2011 12:16 PM

All replies

  • What is happening here is that after you create links between the Customer and item1 and then item2, SaveChanges sends the POST requests that create these new related objects in the data source. Then, LoadProperty loads the two newly created Item objects into the c.Items collection.

    When you call DeleteObject(), item2 is marked for deletion in the context; it is not also removed from the Items collection. Calling LoadProperty() then loads any objects missing from the collection, but it doesn't remove ones that shouldn't be there. To keep the collection in sync with the context, you should call c.Items.Remove(item2).

    Also, you might consider instead calling the DataServiceContext.AddRelatedObject() method, which replaces AddObject() and AddLink(). You may also want to call c.Items.Add(Item1) to make sure that the Items collection has all the Items, and then you won't need to call LoadProperty() (unless other clients are adding Items)--so that saves you a request to the data service just to load the Items collection.

    Cheers,

    Glenn Gailey


    This posting is provided "AS IS" with no warranties, and confers no rights.

    Monday, April 11, 2011 5:54 AM