none
How Can I Load Two Query Paths Using the DbContext.Entry(x).Reference(y => y.x) Pattern... RRS feed

  • Question

  • Trying to figure out how to load two query paths using the DbContext.Entry(x).Referece(y => y.xyz) pattern.

    For example, assume the following model…

    Suppose you execute a query to retrieve Employee 1 from the right-most entity:

                    var employee = context.Employee.Where(x => x.Id == 1);

    Then, in a separate single query, you want to retrieve her department and company information, without re-querying Employee.

    I can easily get her department with the following construct:

                    var department = context.Entry(employee).Reference(x => x.Department).Query();

    However, am having trouble getting her Company information (in the same query as Department). Again my goal is to retrieve her Department and Company information only, in a single query.

    Any ideas on how to set up the construct to retrieve Company information, as well?


    robert vettor

    Friday, May 24, 2013 9:39 PM

Answers

  • Hi Rob;

    The following code snippet should do what you need. Please note that you must disable Lazy Loading otherwise the collection pointed to by the entities will also get downloaded.

    context.Configuration.LazyLoadingEnabled = false;
    var deptComp = context.Entry(employee).Reference(x => x.Department).Query().Select(dc => new { Department = dc, Company = dc.Company}).ToList();

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Saturday, May 25, 2013 3:16 AM
  • Hi Rob,

    I think you can also try to combine explicitly loading with eagerly loading:

    var department = context.Entry(employee).Reference(x => x.Department).Query().Include(d => d.Employees).FirstOrDefault();

    Best regards,

    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 28, 2013 7:11 AM
    Moderator

All replies

  • Hi Rob;

    The following code snippet should do what you need. Please note that you must disable Lazy Loading otherwise the collection pointed to by the entities will also get downloaded.

    context.Configuration.LazyLoadingEnabled = false;
    var deptComp = context.Entry(employee).Reference(x => x.Department).Query().Select(dc => new { Department = dc, Company = dc.Company}).ToList();

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Saturday, May 25, 2013 3:16 AM
  • Hi Rob,

    I think you can also try to combine explicitly loading with eagerly loading:

    var department = context.Entry(employee).Reference(x => x.Department).Query().Include(d => d.Employees).FirstOrDefault();

    Best regards,

    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 28, 2013 7:11 AM
    Moderator