views:

196

answers:

1

I have 2 tables that are mapped in my entity model which are basically this

Tasks
(
   TaskId
   TaskName
   Description
   ProjectId (Foreign Key)
)

Projects
(
   ProjectId
   ProjectName
)

I retrieve my task using Linq to Entities like this

Tasks task = (from t in db.Tasks where t.TaskId == id select t).FirstOrDefault();

The task object then has a Projects object which I assume could contain the project this task is related to, but in my case the Projects object is always null. I wouldnt mind if I could at least get the projectId from the tasks object but this seems to be hidden.

Any points on how I should handle this or where I'm going wrong? I'm still trying to get my head round linq and the entity framework.

Thanks

A: 
Tasks task = (from t in db.Tasks.Include("Project") where t.TaskId == id select t).FirstOrDefault();

(replace "Project" with the name of the Project navigation property)

Thomas Levesque