Search code examples
entity-frameworksilverlight-4.0wcf-ria-services

Entity Framework RIA Services How do I include entities within an included entity


I have the following hierarchy:

AccountCatagory

  AccountType

    AccoungGroup

        GeneralLedger   

            SubsidiaryLedger

so each AccountCatagory has AccountTypes, each AccountType has AccountGroups...

Which needs to be loaded in to a tree view:

I need to load everything when the account category is loaded and I wrote it like this:

   public IQueryable<AccountCatagory> GetAccountCatagories()
    {
    return this.ObjectContext.AccountCatagories.Include("AccountTypes");
    }

Which works fine but only loads AccountTypes. within each AccountCatagory.

Writing an Include on each of the entities GetQuery doesn’t work.

How do I tell RIA services to Include entity when the Included entity also has an include/entity to load?


Solution

  • Here is an example of the way I got mine to work, assuming that all of your foreign keys are set up properly and the proper changes have been made in the metadata file of your Domain Service:

    return ObjectContext.Users.Include("AccessRole")
        .Include("AccessRole.AccessRoleReports")
    

    I have never tried including objects with so many subtypes, but I assume this may work:

    return this.ObjectContext.AccountCatagories.Include("AccountTypes")
        .Include("AccountTypes.AccountGroups")
        .Include("AccountTypes.AccountGroups.GeneralLedger")
        .Include("AccountTypes.AccountGroups.GeneralLedger.SubsidiaryLedger");