New Posts New Posts RSS Feed: Force Refresh of Navigation Properties
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Force Refresh of Navigation Properties

 Post Reply Post Reply
Author
ken.nelson View Drop Down
Groupie
Groupie


Joined: 03-Mar-2009
Location: Ann Arbor, MI
Posts: 54
Post Options Post Options   Quote ken.nelson Quote  Post ReplyReply Direct Link To This Post Topic: Force Refresh of Navigation Properties
    Posted: 11-Sep-2009 at 1:28pm
Hi,
 
Is there any way to force a refresh/refetch of a navigation property?  I've tried a few things, and the most promising appeared to be something along the lines of customer.Orders.Reload(MergeStrategy.OverwriteChanges), but it didn't appear to work.
 
I worked around it by removing the entity from the entity manager, then requerying for the same entity, but that seems less than ideal.  Is there a better way of doing this?
 
Thanks,
Ken
Back to Top
GregD View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 374
Post Options Post Options   Quote GregD Quote  Post ReplyReply Direct Link To This Post Posted: 14-Sep-2009 at 2:46pm
Reload() should do it, and it seems to work for me.  I tested it as follows in a console app against NorthwindIB:

    private void DoIt() {
      Order anOrder = _mgr.Orders.FirstOrDefault();
      Console.WriteLine("Order Id = {0}; No. of OrderDetails = {1}", anOrder.OrderID, anOrder.OrderDetails.ToList().Count());
      Console.WriteLine("Pause here and add an OrderDetail to this outside of this program...");
      PromptToContinue();
      anOrder.OrderDetails.Reload(MergeStrategy.PreserveChanges);
      Console.WriteLine("Order Id = {0}; No. of OrderDetails = {1}", anOrder.OrderID, anOrder.OrderDetails.ToList().Count());
      PromptToContinue();
    }

    private void PromptToContinue() {
      Console.WriteLine();
      Console.WriteLine("Press ENTER to continue...");
      Console.ReadLine();
    }

At the first PromptToContinue(), while execution was paused, I manually changed the OrderId in one or more of the records in the OrderDetail backing table, in such a way as either to add or remove an OrderDetail from the OrderDetails collection of anOrder. The second output of the OrderDetails count shows the change. I tested it both for an increased and a decreased collection, thinking that maybe it only reloaded entities it already had, but it does pick up newly added children as well.

Of course, the way Reload handles externally made changes to an entity that's already in the collection depends upon the MergeStrategy you use. If you use PreserveChanges, for example, then in the case of an entity that has changed both in the cache and externally, you would not pick up the external changes.

Back to Top
ken.nelson View Drop Down
Groupie
Groupie


Joined: 03-Mar-2009
Location: Ann Arbor, MI
Posts: 54
Post Options Post Options   Quote ken.nelson Quote  Post ReplyReply Direct Link To This Post Posted: 15-Sep-2009 at 6:47am
The merge strategy we're using is OverwriteChanges, so that shouldn't be the problem. 
 
I can't speak for a win form console application version because I didn't test that.  Is it possible that this feature works in syncronous win forms but not in asynchronous Silverlight apps?  Truthfully I was a bit surprised there wasn't a ReloadAsync() method.
 


Edited by ken.nelson - 15-Sep-2009 at 6:48am
Back to Top
GregD View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 374
Post Options Post Options   Quote GregD Quote  Post ReplyReply Direct Link To This Post Posted: 15-Sep-2009 at 10:21am
Originally posted by ken.nelson

Is it possible that this feature works in syncronous win forms but not in asynchronous Silverlight apps?  Truthfully I was a bit surprised there wasn't a ReloadAsync() method.


Oh, sorry, I wasn't paying attention to which forum we're in.  Of course it would need to be async. Perhaps we've simply not implemented that yet, in which case you'd just have to do a normal async query to refresh the related entities. I'll check into it.
Back to Top
GregD View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 374
Post Options Post Options   Quote GregD Quote  Post ReplyReply Direct Link To This Post Posted: 15-Sep-2009 at 2:04pm
Ken:

I haven't tested this, but I'm told that Reload does work in SL, the same as the usual (implicit) load of related entities – it will be performed asynchronously if the UseAsyncNavigation flag is true on the EntityManager, which it will be for a SL app.  This means that you will either need to have a handler in place for PendingEntityListResolved, or check the IsPendingEntityList flag, to determine when the list is reloaded.

Greg

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down