Problem loading referenced entities in Entity Framework.

Problem loading referenced entities in Entity Framework.

Post by bW9uc3Rlcn » Thu, 18 Jun 2009 03:49:01


I have a nice & simple model, with only a few entities. One of these
entities, (called ArticleCategory), has a link to itself as a parent.
Essentially, ArticleCategory鈥檚 are hierarchical in their nature, i.e. each
one either has another ArticleCategory as its parent, or a 鈥淣ULL鈥?in the ID
column if it is a top level category. The parent category is referenced from
ArticleCategory as ArticleCategory.ParentArticleCategoy.

When I query this model either via an EntityDataSource, or directly via a
linq query, I get the same (seemingly) weird behaviour, which is鈥? If I
select all ArticleCategory entries from the database, the
ParentArticleCategory associated Entity is loaded in all scenarios except two:

1. When the ArticleCategory.ParentArticleCategoryID is NULL in the
database. This is as expected, if the foreign key is null, we would not
expect any object to be pulled in.
2. And this is the problem one... I get a NULL for the entity
reference for ArticleCategory.ParentArticleCategory when the parent of the
ArticleCategory.ParentArticleCategory is NULL in the database. i.e.
ArticleCategory.ParentArticleCategory. ParentArticleCategoryID is null? Why
would this be? The ArticleCategory I have loaded has a valid parent in the
database, but it is not loaded, seemingly because it has a NULL parent?

Here is an example of scenario two above to bring it to life a little鈥?
ArticleID ParentArticleID
1 NULL
2 1
3 2

If I selected all of these entities using linq I would get the following鈥?
- ArticleCategory(3).ParentArticleCategory 鈥?parent object populated, ID is
2 - CORRECT
- ArticleCategory(2).ParentArticleCategory 鈥?parent object is NULL, - WRONG!
鈥?why is this ? It should be 1!
- ArticleCategory(1).ParentArticleCategory 鈥?parent object is NULL, it
should be - CORRECT

Can anyone explain why I get this behaviour?

Interestingly, if I do a linq query against this model, but on the where
clause I add 鈥淧arentArticleCategory. ArticleCategory = 1鈥? it will correctly
return ArticleCategory(2), but its ParentArticleCategory association will be
null, even though I selected it via a where clause based on its
ParentArticleCategory existing with an ArticleCategoryID = 1! e.g.

var articleCats = from articleCat in context.ArticleCategory
where
articleCat.ParentArticleCategory.ArticleCategoryID == 1
select articleCat;

If I perform the same linq query, but change the where clause to
ParentArticleCategory. ArticleCategory = 2, the ArticleCategory returned is
correctly number 3. But in this case, ParentArticleCategory is bound to the
correct object, ArticleCategory = 2 (I assume due to the fact that Article 2
does not have a null parent)

So, the nuts and bolts is this. For a given ArticleCategory I only get the
associated ParentArticleCategory entity loaded if its parent is not null.
Am I doing something wrong, or is there a work around for this?

Many thanks in advance!

Roy