7

I am new to Entity Framework, but might be misunderstanding something, or doing something wrong.

My code, to get me a list of tasks for a particular 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, it seems to get a list of records back, but related items are not loaded. My 'Transformer.UnpackTask' method takes the task entity which I loaded, and then transforms it into a different object which goes up to the UI via the business/service layers.

But as soon as my Unpacker function tries to references an item which is a related object (For example, a task has an 'AssignedPerson', which has a Person entity with person details. But the AssignedPerson property of my entity is NULL. I thought it would load the related items.

Am I misunderstanding?

4 Answers 4

8

You should explicitly include references with the Include() method. It has two overloads: one takes the property as lambda expression, the other takes the path to load as a string (useful when you need to load references on objects available in collections).

MSDN reference

Sign up to request clarification or add additional context in comments.

Comments

2

What you are looking for is .Include you use it like this

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

with .Include your parents (or related records) will get attached and passed across while without they will be nulls.

Comments

0

try this. What we are trying to do here is include the related entity in the select query itself, lazy load. So when you are unpacking the task you should be able to get the related entity aswell.

Things to check:

  • The task entity should have the browse ability to the person entity else this will fail.
  • Add the [Include] keyword on top of the person entities inside the metadata of task in the metadata file in your service layer.

-- E.g. metadata file of the service you created.

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;}
}

the service file

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;
    }

Hope it helps. Cheers

2 Comments

This looks interesting, but as a novice, where is this code?? I can't seem to locate it.
are you writing your own wcf service or building on top of domain services?
0

Also, check you're using the correct Include().

The one from the Microsoft.EntityFrameworkCore namespace, not System.Data.Entity.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.