strongriley
strongriley

Reputation: 1093

Entity Framework 3 - Navigation Property Null with Varying Include()

I have a query method where I sometimes include additional tables "Category" and "Manufacturer" depending on the situation. However, whenever I run the query, the category and manufacturer navigation properties are always blank. What am I doing wrong?

private IQueryable<Item> GetQuery(ItemFilter filter, ItemCacheContainer context)
    {
        //Perform optional Joins
        ObjectQuery<Item> query = context.Items;

        if (filter.JoinCategory)
            query.Include("Category");

        if (filter.JoinManufacturer)
            query.Include("Manufacturer");

        return query.Where(i =>
              (!filter.ItemId.HasValue
              || i.ItemId == filter.ItemId.Value));
    }

Also, here's how I'm using the GetQuery method but when I put a breakpoint in ConvertItemFromCache I see those null navigation properties.

GetQuery(filter, context)
    .ToList()
    .ConvertAll(ConvertItemFromCache)
    .SingleOrDefault();

Thanks!

Upvotes: 1

Views: 692

Answers (1)

Aducci
Aducci

Reputation: 26694

You need to set query to the result of query.Include("...")

query = query.Include("Category");

So in your example:

if (filter.JoinCategory)
 query = query.Include("Category");

if (filter.JoinManufacturer)
 query = query.Include("Manufacturer");

Upvotes: 3

Related Questions