views:

396

answers:

2

I'm new to EF and I have two tables, the first called ContestEntry and the second Items which has a one to many relationship with ContestEntry. I'm trying to access Contest Entries and then from there get the related Item information. Here is the code I'm using.

ContestEntry cEntry =
        Ctx.ContestEntries.Include("Item").Where(ce => ce.ID == 4).First();

The problem is that the Item relationship is NULL when I look at it in the debugger.

I'm probably missing something just not sure. Any help would be appreciated.

Thank You!

+2  A: 

In your question, you mention the seconds table is "Items", yet in your LINQ query, you're including "Item" - just a typo??

Also, another way to load a referenced entity would be this:

ContestEntry cEntry =
    Ctx.ContestEntries.Include("Item").Where(ce => ce.ID == 4).First();

if(!cEntry.Items.IsLoaded)
{
   cEntry.Items.Load();
}

If you have a 1:n navigational property, you should be able to check whether it's been loaded, and if not, load it on demand when you need it.

Marc

marc_s
yes, that was just a typo, it should be "Item". Thank You!
Paul
Also, I'm not able to call the IsLoaded function for the cEntry object when I follow the code you suggested. The function doesn't show up in intellisense either. Thanks!
Paul
@Paul: the "IsLoaded" is **not** on the cEntry - it's on the `cEntry.Item` (or should be) - if it's a 1:n association between an Entry and multiple `Item` elements.
marc_s
A: 

The solution given is a total nonsense and rubbish. cEntry.Items is null (mentioned by the poster), how can you call the isLoaded() function. Also isLoaded() function is not defined anywhere in .Items.

Tim Chan