Force the load collection linq

Hi!

I've 2 tables

by exemple:

Categories and Models

One categories has many Models
One models has only one categories(so we've a foreign key in models)

So I use LINQ to Entity, but I've a problem.

If I've a category object and I do:

category.Models.count();

I've "0", and in the database, I've correct things.

And if I debug when I do this, a property named "isLoaded" that have the value "false".

So I think the collection isn't loaded.

Must we do anything special? How do we load this collection?

Thanks!
LVL 1
NargzulAsked:
Who is Participating?
 
NargzulAuthor Commented:
I've used the ".Include("blabla")".

It's exactly what I need
0
 
CoveyCraigCommented:
Read this article on loading categories:  http://blogs.msdn.com/meek/archive/2008/12/07/stored-procedures-and-first-class-relationships-in-the-entity-framework.aspx

Load using this syntax:  Category models = context.Categories.Where(c => c.Name == "Model").First();

0
 
naspinskiCommented:
You might want to do some eager loading - I explain it here: http://naspinski.net/post/You-might-be-making-a-lot-of-unneccessary-trips-to-your-DB-with-Linq-to-SQL.aspx
dbDataContext db = new dbDataContext();
DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith<category>(i => i.Model);
db.LoadOptions = dlo; 

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.