Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have two tables, Kittens and Owners in my Entity Framework model. A Kitten has 1 Owner but an Owner can have many Kittens.

I have a repository method called GetKittens() that returns _context.Kittens.ToList();

Since I set up an association, I can do kitten.Owner.Name.

But since ToList() was already called, and the context disposed of, how does it access the property? When retrieving an Entity, does it do a Join to all tables that have an association?

I have to write a query that pulls data from 4 tables, so I am wondering how to do this efficiently, hence this question trying to understand a bit more about how EF works.

share|improve this question
Try running a db trace while you run the code - you'll see what is running when, which should help make things clearer. – Jon Egerton 8 mins ago
ToList () will only give you your kittens. You need to Select them and Include (kitten => kitten.Owner).ToList (). – retailcoder 4 mins ago
Best project your selection into a type that includes the kittens and the properties of owner you're interested in. – retailcoder 3 mins ago
I'm confused because I am able to access Owner without doing any Include – SLC 1 min ago

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.