本文介绍了IObjectSet包含CompiledQuery的扩展方法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的自定义ObjectContext类中,我的实体集合被公开为IObjectSet,因此可以进行单元测试。当我在一个编译查询中使用这个ObjectContext时,我遇到了一个问题,并调用了Include扩展方法(来自Julie Lerman的博客):

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

返回查询(context,theId);
}

LINQ to Entities不会识别方法'System.Linq.IQueryable 1 [MyPocoObject]包含[MyIncludedPocoObject](System.Linq.IQueryable 1 [MyPocoObject],System.String)'方法,此方法无法转换为存储表达式。 p>

如果我在ObjectSet集合上使用相同的查询,而不是IObjectSet,它可以正常工作。如果我只是运行这个查询而不预编译就可以正常工作。我在这里缺少什么?

解决方案

我确信我刚刚误解了,因为你似乎一直在看这个一段时间 - 但是不应该包括ObjectSet而不是该对象集查询的对象。



例如:

  var query = CompiledQuery.Compile< MyCustomContext,int,IQueryable&MyPocoObject>(
(ctx,id)=> ctx.MyPocoObjects.Include IncludedPocoObjectSET)。其中(n => n.IncludedPocoObject.id == id));

你还可以确认运行相同的查询没有编译不会抛出不能翻译例外?


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?

解决方案

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?

这篇关于IObjectSet包含CompiledQuery的扩展方法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 02:11