views:

91

answers:

1

I have setup 4 separate tables that have fk relationships between all of them. The main problem I am having is between a table called EmployeeQuestions and Questions tables. I am using Entity to model the objects. When I get my EmployeeQuestions object I check it to make sure that the relationship between the EmployeeQuestions and Employee is there and I can get all the information through that relationship however the relationship between EmployeeQuestions and Questions is always null. I have checked to make sure that the relationship was setup correctly and it is. Also I have tried to do a search on a list of all EmployeeQuestions and I can query those results for the QuestionID in the Questions table however I cannot get the description field.

I am very new to working with databases at all, any help would be much appreciated.

Thanks.

+1  A: 

Can you show us the query you're using?

By default, Entity Framework will not traverse associations to grab the objects "on the other end" of the association - you will have to do this explicitly.

var emps = from e in ObjectContext.Employees 
           where (some condition)
           select e;

In this case, you "Employee.Questions" association (assuming it's called that) would always be NULL - by design.

You can specify an "Include" in your query:

var emps = from e in ObjectContext.Employees.Include("Questions") 
           where (some condition)
           select e;

and now in this case, when fetching an employee, the associated "Questions" would also be loaded.

Or you can do:

var emps = from e in ObjectContext.Employees 
           where (some condition)
           select e;

foreach(Employee e1 in emps)
{
  if(!e1.QuestionReference.IsLoaded)
  {
     e1.QuestionReference.Load();
  }
}

to explicitly and in code load the associated objects.

Since this has been regarded by a lot of people as a shortcoming, in EFv4, there will be an option to turn on this automatic "deferred" loading on - but again, you have to do it explicitly and knowingly.

Marc

marc_s
Thank you so much that was really frustrating.
J Lundberg