Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IObjectSet Include Extension Method Errors with CompiledQuery

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?

like image 946
Paul Avatar asked Sep 08 '26 17:09

Paul


1 Answers

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

like image 141
Julie Lerman Avatar answered Sep 10 '26 07:09

Julie Lerman