Deep Load Child Objects

3 posts, 0 answers
  1. G. Deward
    G. Deward avatar
    9 posts
    Member since:
    Jan 2012

    Posted 18 Jan 2012 Link to this post

    How can I deep load parent-child objects?  For example, if I have Country, State, and City tables, how I can I load all Countries and all States for all Countries and all Cities for all States?  I have played with ORM for over a day now and cannot figure it out.  I do not know if this is a LINQ question or ORM question.

    Thank you -
    - Greg D. 
  2. G. Deward
    G. Deward avatar
    9 posts
    Member since:
    Jan 2012

    Posted 18 Jan 2012 Link to this post

    I have tried the following without any success:

                using (var entities = new GlobalResourcesEntities())

                {

                    FetchStrategy fetchStrategy = new FetchStrategy();

                    fetchStrategy.LoadWith<State>(c => c.Country);

                    fetchStrategy.LoadWith<City>(c => c.State);

                    //fetchStrategy.LoadWith<State>(c => c.CountryID);

                    //fetchStrategy.LoadWith<City>(c => c.StateID);

                    entities.FetchStrategy = fetchStrategy;

                    try

                    {

                        var countries = entities.Countries.ToList();

                        foreach (var country in countries)

                        {

                            Console.WriteLine(country.CountryName);

                        }

                    }

                    catch (Exception ex) { Console.WriteLine(ex.Message); }

                }

  3. DevCraft banner
  4. Ralph Waldenmaier
    Admin
    Ralph Waldenmaier avatar
    202 posts

    Posted 19 Jan 2012 Link to this post

    Hello Greg,

    The provided code snippet looks good and should load all the State objects when you query the Countries. This is nearly the same scenario as described in our documentation.
    But from the information you provided, I can not see where an error occurs. Which error did you get? What exactly is not working in your setup? Did you look into a log file in order to check whether the expected sql statement is fired? 

    I am looking forward to hearing from you soon in order to get this solved for you.

    Regards,
    Ralph
    the Telerik team

    SP1 for Q3’11 of Telerik OpenAccess ORM is available for download

Back to Top