3

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/) and in her book Programming Entity Framework 2nd edition on pages 722-723. Here is the code:

Query:

public class CommunityPostsBySlugQuery : QueryBase<IEnumerable<CommunityPost>>
    {
        private static readonly Expression<Func<Database, string, IEnumerable<CommunityPost>>> expression = (database, slug) => database.CommunityPosts.Include("Comments").Where(x => x.Site.Slug == slug).OrderByDescending(x => x.DatePosted);
        private static readonly Func<Database, string, IEnumerable<CommunityPost>> plainQuery = expression.Compile();

        private static readonly Func<Database, string, IEnumerable<CommunityPost>> compiledQuery = CompiledQuery.Compile(expression);

        private readonly string _slug;
        public CommunityPostsBySlugQuery(bool useCompiled, string slug): base(useCompiled)
        {
            _slug = slug;
        }

        public override IEnumerable<CommunityPost> Execute(Database database)
        {
            return base.UseCompiled ? compiledQuery(database, _slug) : plainQuery(database, _slug);
        }
    }

Extension

public static class ObjectQueryExtension
    {
        public static IQueryable<T> Include<T>(this IQueryable<T> source, string path)
        {
            var objectQuery = source as ObjectQuery<T>;
            return objectQuery == null ? source : objectQuery.Include(path);
        }
    }

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 think your page numbers are wrong, 272-273 maybe? Commented Nov 16, 2010 at 19:54

2 Answers 2

1

I really don't know but have asked if someone on the EF team can answer it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for taking the time to ask around. Been struggling with this for a month now. Would be great to get an answer!
1

Response by EF Team:

This is a known issue with CTP4, Include is an instance method on ObjectSet but when your set is typed as IObjectSet you are actually using an extension method on IQueryable that is included in CTP4. This extension method doesn't work with compiled queries but we will try and support this in the next release.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.