IObjectSet 包含 CompiledQuery 的扩展方法错误

发布于 2024-10-02 09:10:30 字数 2124 浏览 3 评论 0原文

在我的 Custom ObjectContext 类中,我将实体集合公开为 IObjectSet,以便可以对它们进行单元测试。当我在编译的查询中使用此 ObjectContext 并调用“包含”扩展方法时,我遇到了问题(来自 Julie Lerman 的博客 http://thedatafarm.com/blog/data-access/agile-entity-framework-4-repository-part-5-iobjectset/ )以及她的书《编程实体框架》第二版第 722-723 页。 下面是代码:

查询:

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);
        }
    }

扩展

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 使用相同的查询,它可以正常工作。如果我只是运行这个查询而不进行预编译,它就可以正常工作。我在这里缺少什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开半夏魅人心 2024-10-09 09:10:30

我真的不知道,但我问过 EF 团队是否有人可以回答。

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

一身软味 2024-10-09 09:10:30

EF 团队的回复:

这是 CTP4 的一个已知问题,Inclusion 是 ObjectSet 上的一个实例方法,但是当您的集合键入为 IObjectSet 时,您实际上是在使用扩展方法CTP4 中包含的 IQueryable。此扩展方法不适用于已编译的查询,但我们将尝试在下一个版本中支持此功能。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文