Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top