【问题标题】:Generic Include on ObjectQuery doesn't actually include anythingObjectQuery 上的通用包含实际上不包含任何内容
【发布时间】:2012-10-15 16:16:21
【问题描述】:

今天我尝试修改现有的通用 GetAll 方法以使用急切的包含。 我启用了延迟加载,但我想为一些特定的缓存优化做预先加载。

  public IQueryable<T> GetAll<T>(List<string> eagerIncludeList) where T : EntityObject, new()
    {
        var entitySet = String.Format("[{0}]", FetchEntitySetName<T>());
        ObjectQuery<T> q = this.db.CreateQuery<T>(entitySet);



        foreach (string x in eagerIncludeList) {
            q.Include(x);
        }
        return q;
    }

上面的代码不起作用(至少没有预先加载),用例如调用它

DB.GetAll<PageConfig>(new List<string>() {"Containers"})

不为我的 PageConfigs 加载相关的容器, 但是 在对包含进行硬编码时,它确实有效,所以我确定“容器”这个名称是正确的

var example = db.PageConfigSet.Include("Containers").ToList();

这给出了 example.First().Containers.IsLoaded = true;

关于为什么使用 CreateQuery 的通用代码不起作用,但使用硬编码 ObjectSet 的代码不起作用的任何想法?

【问题讨论】:

    标签: c# generics entity-framework-4 eager-loading


    【解决方案1】:

    q.Include(x) 返回一个包含该表的新 ObjectQuery&lt;T&gt;

    你没有对返回值做任何事情。

    【讨论】:

    • 射击我!我已经看了1个小时了!所以 Include 实际上并没有修改输入......好吧!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多