1


with entity framework, how should I load object from an object list ? Example.

public class Phone
{
    public int Id { get; set; }
    public string Type { get; set; }
    public Corporation Corp { get; set; }
}

public class Corporation 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Phone> Phones { get; set; }
}

// And now, when I get a Corporation class from entity framework,
//I can't access the Phones. I get an error: The value canot be null.
_db.Corporations.First().Name; // Works
_db.Corporations.First().Phones.First().Type; // Fails

How should I load the List properties ?

1 Answer 1

3

mark the related entities as virtual to enable lazy loading by default.

class Corporation
{
   public int Id {get;set;}
   public virtual List<Phone> Phones {get;set;}
}
class Phone
{
   public int Id {get;set;}
   public virtual Corporation Corp {get;set;}
}
Sign up to request clarification or add additional context in comments.

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.