简体   繁体   English

实体框架3 - 导航属性为空且包含不同的包含()

[英]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方法,但当我在ConvertItemFromCache放置断点时,我看到那些空导航属性。

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

Thanks! 谢谢!

You need to set query to the result of query.Include("...") 您需要将query设置为query.Include("...")

query = query.Include("Category");

So in your example: 所以在你的例子中:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM