1
votes

In a current MVC4.0 project I am using Entity Framework 4.1 Database first model.

Part of this structure includes the following tables

compGroupData SurveyData SecondaryData

compGroupData and SurveyData are not joined in the database

SecondaryData is joined to SurveyData on a one to one relationship via a Foreign Key SurveyData.surveydatakey = SecondaryData.surveydatakey

In my project I have a class ComparisonWithData defined as:

public class ComparisonWithData
{
    public compGroupData compgrp { get; set; }
    public SurveyData surveydata { get; set; }
    public ComparisonWithData()
    {
        compgrp = new compGroupData();
        surveydata = new SurveyData();
    }
}

This gives me a result set for a specific Comparison group and the data that matches this.

In the past I have retrieved the data for this via the following query:

    List<ComparisonWithData> comparisonwithdata  = ((from compgrp in db.compGroupDatas
                       where compgrp.grpYear == rptyear && compgrp.CompGroupID == ccompgrp.CompGrpID
                       join surveydata in db.SurveyDatas on new { compgrp.companyid, SurveyYear = (Int32)compgrp.SurveyYear } equals new { companyid = surveydata.companyid, SurveyYear = surveydata.surveyyear }
                       select new ComparisonWithData
                       {
                           compgrp = compgrp,
                           surveydata = surveydata,


                       }
                       )).ToList();

With a recent change in data I now need to also reference the SecondaryData but due to the number of records really need this to load Eagerly instead of Lazy. (Lazy loading during the loop results in thousands of DB calls)

I have looked at using the "Include" method on surveydata as well as casting the initial query as an ObjectQuery and doing the Include off that.

The first method doesn't eager load and the second method seems to always return a null object as a result.

Is there a method to Eager load the SecondaryData for SurveyData or should I be looking at a different approach all together.

My only restriction on this is that I can't go up to EF5 because of a limitation we have on .Net 4.5

Any assistance would be greatly appreciated.

Thank you.

1

1 Answers

0
votes

You could try to project into an anonymous object first and use also SecondaryData in that projection, materialize this result and then project again into your final result object. Automatic Relationship Fixup that the EF context provides should populate the navigation property surveyData.SecondaryData of your ComparisonWithData object (as long as you don't disable change tracking in your query):

var data = (( // ... part up to select unchanged ...
           select new // anonymous object
           {
               compgrp = compgrp,
               surveydata = surveydata,
               secondarydata = surveydata.SecondaryData
           }
           )).AsEnumerable();
           // part until here is DB query, the rest from here is query in memory

List<ComparisonWithData> comparisonwithdata =
           (from d in data
           select new ComparisonWithData
           {
               compgrp = d.compgrp,
               surveydata = d.surveydata
           }
           )).ToList();