Entity Framework does not load related objects

I am new to Entity Framework, but maybe something is misunderstood or something is wrong.

My code to get a list of tasks for a specific person:

 public List<TaskObject> GetAssignedTasks(int personId)
        {
            var items = (from s in _te.tasks where s.person.person_id == personId select s).ToList();
            var tasks = new List<TaskObject>();
            foreach (var t in items)
            {

                TaskObject tk = Transformer.UnpackTask(t);

                tasks.Add(tk);
            }
            return tasks;
        }

My problem is that it returns a list of records, but related items are not loading. My Transformer.UnpackTask method accepts the task object that I downloaded, and then converts it to another object that goes to the user interface through the business / service levels.

Unpacker , (, "AssignedPerson", Person . AssignedPerson NULL , .

?

+5
3

Include(). : , (, , ).

MSDN

+4

. , , , . , , .

:

  • .
  • [Include] .

- . .

tasks metadata
 {
 ... 
 ... 

  \\these two should already be there you will just have to add the Include and
  \\ Key attribute.

    [Include]
    public EntityCollection<Person> Person {get; set;}
    [Key]  \\ that connects the task and person entity (FK/PK)
    public int PersonID {get;set;}
}

public List<TaskObject> GetAssignedTasks(int personId)
    {
        var items = (from s in _te.tasks.Include("Person") where s.person.person_id == personId select s).ToList();
        var tasks = new List<TaskObject>();
        foreach (var t in items)
        {

            TaskObject tk = Transformer.UnpackTask(t);

            tasks.Add(tk);
        }
        return tasks;
    }

, .

0

, , .Include

var recordWithParentAttached = db.Tasks.Include(o => o.Person).Single(o => o.person_id == personId);

.Include ( ) , nulls.

0

All Articles