Problem loading referenced entities in Entity Framework.



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’s are hierarchical in their nature, i.e. each
one either has another ArticleCategory as its parent, or a “NULL†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
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
- 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 “ParentArticleCategory. 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
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!


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question