In my Custom ObjectContext class, I have collections of entities exposed as IObjectSet so that they can be checked for unity. I have a problem when I use this ObjectContext in a compiled request and call the “Include” extension method (from Julie Lerman's album http://thedatafarm.com/blog/data-access/agile-entity-framework-4-repository-part -5-iobjectset / ) and in her book, Entity Framework Programming 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 'System.Linq.IQueryable1 [MyPocoObject] Include [MyIncludedPocoObject] (System.Linq.IQueryable1 [MyPocoObject], System.String) .
ObjectSet, IObjectSet, . , . ?