views:

162

answers:

1

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?

A: 

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?

Basiclife
@BasiclifeHere is an example of a query that throws when using IobjectSet and does not when using ObjectSet private static readonly Expression<Func<Database, string, IEnumerable<SiteAnnouncement>>> expression = (database, slug) => database.SiteAnnouncements.Include("UserCreated").Where(sa => sa.Site.Slug == slug);private static readonly Func<Database, string, IEnumerable<SiteAnnouncement>> compiledQuery = CompiledQuery.Compile(expression);
Paul
Complete Error: LINQ to Entities does not recognize the method 'System.Linq.IQueryable`1[PostHope.Core.DomainObjects.SiteAnnouncement] Include[SiteAnnouncement](System.Linq.IQueryable`1[PostHope.Core.DomainObjects.SiteAnnouncement], System.String)' method, and this method cannot be translated into a store expression
Paul
Ok, I may be a little out of my depth so apologies if this is nonsensical: When you refer to the objectset via the interface, it's the definition of the methods on the interface that is presented, not of the methods on the object - Are there any special attributes or compiler hints on the objectset's methods/properties after compilation (check with reflector) that aren't present on the interface?
Basiclife
Will try that. Thanks for taking the time to respond!
Paul