Link to home
Start Free TrialLog in
Avatar of dyarosh
dyarosh

asked on

Lazy Loading not working in EF on all tables

I have a web application that uses EF 5.0.  The main object that I am retrieving is the REF_Referrals object.  Each object maps to a physical table in the database.  

The problem that I am having is when I retrieve the data from the REF_Referrals table using EF, not all of the tables are being loaded.  For example, the REF_ReferralComments and REF_ReferralStatus data is being loaded but the REF_ReferralType is not.  In addition, the REF_Status data is not being loaded within the REF_ReferralStatus object.  Here is the code that I am using to retrieve the data:
            var record = _db.REF_Referrals.Find(referralId);

Open in new window


Here are the Navigation Properties defined on REF_Referrals object and objects that it references:
REF_Referrals has the following navigation properties:
REF_ReferralType
REF_ReferralStatus
REF_ReferralComments

REF_Referrals has the following columns (relevant to my problem):
ReferralsID
REF_ReferralTypeReferralTypeID

The REF_ReferralType object has the following navigation properties:
REF_ReferralGroup
REF_Referrals

REF_ReferralType has the following columns (relevant to my problem):
ReferralTypeID
REF_ReferralGroupReferralGroupID

REF_ReferralGroup object has the following navigation properties:
REF_ReferralType
REF_ReferralGroupHoursOfOperation
REF_SMEs

The REF_ReferralGroup has the following columns (relevant to my problem):
ReferralGroupID
The REF_ReferralStatus object has the following navigation properties:
REF_Status
REF_Referrals

REF_ReferralStatus has the following columns (relevant to my problem):
ReferralsStatisID
REF_StatusStatusID
REF_ReferralsReferralsID

The REF_Status object has the following navigation properties:
REF_ReferralStatus

REF_Status has the following columns (relevant to my problem):
StatusID

The REF_ReferralComments object has the following navigation properties:
REF_Referrals

REF_ReferralComments has the following colums (relevant to my problem):
ReferralCommentID
REF_ReferralsReferralsID

Open in new window


I've looked at the physical records and they all appear correct.  Can anyone tell me why all the data is not being loaded?  I'm including an image where if you look closely you can see how some of the data is loaded but not all of it after the call to FInd.  Any help is greatly appreciated.

User generated image
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Have a look at your REF_Referrals Model - for Lazy Loading to work, the navigation properties need to be defined as virtual, something like this:

public virtual ReferralType REF_ReferralType { get; set; }

Open in new window

Avatar of dyarosh
dyarosh

ASKER

This is what is in the REF_Referrals.cs class:

        public virtual REF_ReferralType REF_ReferralType { get; set; }
        public virtual ICollection<REF_ReferralStatus> REF_ReferralStatus { get; set; }
        public virtual ICollection<REF_ReferralComments> REF_ReferralComments { get; set; }
OK. Not really possible to be specific here without knowing a lot more about how you've configured EF (model attributes etc.) and also seeing your data.

May sound obvious, but are you sure your record actually has a ReferralType, and that the Null value showing isn't actually valid. How have you configured the foreign key relationship between the two entities. Also bear in mind that lazy loading will only retrieve the information from the database when the property is accessed (the whole point of lazy loading)

It really could be a number of things and I can't give you an exact answer at the moment
Avatar of dyarosh

ASKER

1. Yes, the record does have a Referral Type record and isn't null.
2. The REF_Referrals table has a Foreign Key, REF_ReferralTypeReferralTypeId
3. The EF REF_Referrals object has REF_ReferralTypeReferralTypeID defined as a property
4. I've uploaded an image of the properties screen on the Association

What model attributes should I be looking at?
Avatar of dyarosh

ASKER

I had a problem uploading the file so I had to do it separately.
REF_ReferralsAssociations.fw.png
Unfortunately I can't see the problem from the limited information you've given. Without be able to look through the code properly, I'm stumped and can't really advise any further.

Hopefully someone else will come along that can shed some light on the issue.
Avatar of dyarosh

ASKER

I have done more investigating and found the following:

I am using unit testing framework MSTest.  When I run the unit test and do the Find against the database of the newly created referral, some of the navigation properties are not being populated.

If I modify my unit test to find an existing record, all navigation properties are loaded correctly.

If I use my existing application (not the one that I am working on) to find the record, the information is returned properly.

I compared the create and save code from the app I am working on and the production app and they appear to be exactly the same.  I compared the models and they also appear to be the same.

Any ideas on where I should be looking?
As there are so many moving parts to an EF project it's very difficult to know precisely what the problem is. When you create a new record, how are you setting the Navigations properties.

For example, are you pulling the full foreign entity out of the database and then setting the actual navigation property to that, or are you just setting the foreigh key value (i.e the ID)
Avatar of dyarosh

ASKER

I think I discovered the problem.  I am doing an Insert followed by a Find.  Since the Insert puts the record in the local "store" when I do the Find it pulls the data from there.  Do you know if there is a way to force EF to go out to the Database when retrieving data?
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dyarosh

ASKER

Running all on the same context
Avatar of dyarosh

ASKER

I would like to award the points to Chris Stanyon.  He was helpful in finding the solution.