IObjectSet Include Extension Method Errors with CompiledQuery

☆樱花仙子☆ 提交于 2019-12-11 06:07:38

问题


In my Custom ObjectContext class I have my entity collections exposed as IObjectSet so they can be unit-tested. I have run into a problem when I use this ObjectContext in a compiled query and call the "Include" extension method (From Julie Lerman's blog http://thedatafarm.com/blog/data-access/agile-entity-framework-4-repository-part-5-iobjectset/):

    public IQueryable<MyPocoObject> RunQuery(MyCustomContext context, int theId)
    {
        var query = CompiledQuery.Compile<MyCustomContext, int, IQueryable<MyPocoObject>>(
             (ctx, id) => ctx.MyPocoObjects.Include("IncludedPocoObject").Where(n => n.IncludedPocoObject.id == id));

        return query(context, theId);
    }

LINQ to Entities does not recognize the method 'System.Linq.IQueryable1[MyPocoObject] Include[MyIncludedPocoObject](System.Linq.IQueryable1[MyPocoObject], System.String)' method, and this method cannot be translated into a store expression.

If I use this same query on ObjectSet collections rather than IObjectSet it works fine. If I simply run this query without precompiling it works fine. What am I missing here?


回答1:


I'm sure I've just misunderstood as you seem to have been looking at this for a while - But shouldn't you be including the ObjectSet not the object that is queried BY that object set.

eg:

var query = CompiledQuery.Compile<MyCustomContext, int, IQueryable<MyPocoObject>>(
     (ctx, id) => ctx.MyPocoObjects.Include("IncludedPocoObjectSET").Where(n => n.IncludedPocoObject.id == id));

Can you also confirm that running the same query without compiling doesn't throw the "can't translate" exception?



来源:https://stackoverflow.com/questions/2655886/iobjectset-include-extension-method-errors-with-compiledquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!