locked
How to lazy load associated entity on client? RRS feed

  • Question

  • Hello! I am using the July Preview and try to set up some lazy loading.

    I have two entities, User and Profile. At the client, I have loaded a list of Users using GetUser() method. I also have another method GetProfile(Guid userid) which used to retrieve different Users' Profile.

    In my metadata of User class, I have association like this:

    1    [Association("User_Profile", "UserId", "UserId")]
    2    public IEnumerable Profile { get; set; }
    

    I did not use [Include] as I do not want the profile be eager-loaded.

    At the client I tried to load the Profile of a user:

    1    EntityQuery query = context.GetProfileQuery(user.UserId);
    2    LoadOperation load = context.Load(query, MergeOption.OverwriteCurrentValues, loaded_userprofile, user);
    

      
    My question is, after loading, the new profile doesn't goes to my user.Profile nor user._profile. The value of user._profile still have a entityref with the Entity as null. I tried to do in loaded_userprofile callback: 
                 user.Profile = load.Entities.First();
    But it gives me an error saying the entity list does not support the 'Add' operation.

    I read an article the loading should be automatically bounded by RIA service. Am I doing anything wrong?

    Thanks in advance!

     

    Monday, September 7, 2009 6:31 AM

Answers

  • If you are interested you should check my blog as I have documented EntityCollection and EntityRef. In short, the EntityRef is supposed to be monitoring the Profile EntityList for any changes and using the FilterProfile method to find the one Profile that matches. Are you sure that you are loading your profiles into the same DomainContext instance as your original users? If you were then you should have seen the filters firing.

    Wednesday, September 9, 2009 8:27 AM

All replies

  • Have a look at the ExternalExttribute in the documentation and the use of linked domain contexts on the client. You can decorate an association with "[External]" and chain the associated domain context via AddReference. See this thread http://silverlight.net/forums/t/118760.aspx

    Tuesday, September 8, 2009 2:53 AM
  • Hi Theo,

    I am not sure I understand this. But my User and Profile Entity are defined in one edmx. And the GetUser() and GetProfile(Guid userid) are both exposed in the same domainservice. Does that imply they are in one domain context?

    If they are both in the same domain context, do I still use [external] to bind their data together as you described? I thought in one domain context I can expose as many entity as I wanted to and still be able to associate them together.

    Thank you!

     

    Tuesday, September 8, 2009 4:36 AM
  • Open up the generated file and put a breakpoint on FilterProfile (or FilterProfiles, depends if it is 1:1 or 1:Many). When the profile loads you should see your breakpoint fire and then you can see if it is returning a true or a false.

    Tuesday, September 8, 2009 9:36 AM
  • Thanks Colin! I found the FilterProfile but it's never called. Because right after the GetUser()query return, each of the User object already has a EntityRef<Profile> in its _profile member but the EntityRef<Profile> only has null as its Entity value.

    The generated code of my Profile's getter is:
    get
    {
    if ((this._profile == null)){
        this._profile = new EntityRef<Profile>(this, "Profile", this.FilterProfile);}
    return this._profile.Entity;}

    So it always skips the filter as my _profile is not null.

    It seems whenever I query my list of users, it attached automatically an empty EntityRef<Profile> to _profile and make the getter fail.

    I have looked examples about May Preview using the LoadProfile() method and it seems to work easily. But in July Preview everything about Loading Query is changed and I cannot find any samples about query in associated entities.

    Thank you in advance.

    Wednesday, September 9, 2009 3:59 AM
  • If you are interested you should check my blog as I have documented EntityCollection and EntityRef. In short, the EntityRef is supposed to be monitoring the Profile EntityList for any changes and using the FilterProfile method to find the one Profile that matches. Are you sure that you are loading your profiles into the same DomainContext instance as your original users? If you were then you should have seen the filters firing.

    Wednesday, September 9, 2009 8:27 AM
  • Thanks! But my User object just has a empty EntityRef attached to the _profile automatically, making the whole filtering mechanism not working. If the _profile remains to be null, what you said will work.

    I do not how to set up the entities so that after the load it will not stuff a null Entity to the associated property.

    Friday, September 11, 2009 5:20 AM
  • Thanks! But my User object just has a empty EntityRef attached to the _profile automatically, making the whole filtering mechanism not working. If the _profile remains to be null, what you said will work.

    I do not how to set up the entities so that after the load it will not stuff a null Entity to the associated property.

    No, you aren't understanding. The EntityRef has an active filtering mechanism, once it is created it is always filtering on the associated EntityList. You seem to think that it is a one time thing, the EntityRef is created, it is told to filter, and if it doesn't find anything then it is a null ref and that is it. That isn't the case.

    You can't setup the entity to keep the EntityRef null, and you wouldn't want to.

    Friday, September 11, 2009 8:14 AM
  • Thanks Colin! I've found what's wrong finally.
    As you mention, it doesn't detect because they are not in the same context.
    I keep declaring new context everytime I use it.  And it seems I can create as many domaincontext as I want in the scope of an application and they will manage the entities separately.
    They're working nicely now. Thanks a lot! :)

    Saturday, September 12, 2009 3:42 AM