带有 Sql Compact Edition 4.0 的 Entity Framework 4.0 - 未实现异常

发布于 2024-10-31 17:52:05 字数 1544 浏览 0 评论 0原文

我有这个 LINQ 查询:

    var children = DataContext.Entities.Nodes
                    .Where(n => n.Parent.Name == node.Key)
                    .OrderBy(n => n.SortOrder);

    foreach (var child in children)
        var childNode = CreateNode(child);

使用 SQL Server 时,一切正常。但是,当使用 SqlCe 时,我收到以下错误:

[SqlCeException (0x80004005): Not implemented]
   System.Data.SqlServerCe.SqlCeDataReader.ProcessResults(Int32 hr) +125
   System.Data.SqlServerCe.SqlCeDataReader.IsEndOfRowset(Int32 hr) +131
   System.Data.SqlServerCe.SqlCeDataReader.Move(DIRECTION direction) +376
   System.Data.SqlServerCe.SqlCeDataReader.Read() +95
   System.Data.Common.Internal.Materialization.Shaper`1.StoreRead() +44

[EntityCommandExecutionException: An error occurred while reading from the store provider's data reader. See the inner exception for details.]
   System.Data.Common.Internal.Materialization.Shaper`1.StoreRead() +130
   System.Data.Common.Internal.Materialization.SimpleEnumerator.MoveNext() +46

知道这里发生了什么吗?

我什至尝试在 foreach 之前调用 ToArray(),但这没有帮助。

编辑:

如果我将查询更改为:

    var children = DataContext.Entities.Nodes
                    .Where(n => n.Parent.Name == node.Key)
                    .ToArray()
                    .OrderBy(n => n.SortOrder);

它有效...为什么?

编辑2:顺便说一句,Parent导航器指向同一个表,因此每个Node可以有{0..1}父Node

I have this LINQ query:

    var children = DataContext.Entities.Nodes
                    .Where(n => n.Parent.Name == node.Key)
                    .OrderBy(n => n.SortOrder);

    foreach (var child in children)
        var childNode = CreateNode(child);

When using SQL Server, everything works fine. However, when using SqlCe, I get the following error:

[SqlCeException (0x80004005): Not implemented]
   System.Data.SqlServerCe.SqlCeDataReader.ProcessResults(Int32 hr) +125
   System.Data.SqlServerCe.SqlCeDataReader.IsEndOfRowset(Int32 hr) +131
   System.Data.SqlServerCe.SqlCeDataReader.Move(DIRECTION direction) +376
   System.Data.SqlServerCe.SqlCeDataReader.Read() +95
   System.Data.Common.Internal.Materialization.Shaper`1.StoreRead() +44

[EntityCommandExecutionException: An error occurred while reading from the store provider's data reader. See the inner exception for details.]
   System.Data.Common.Internal.Materialization.Shaper`1.StoreRead() +130
   System.Data.Common.Internal.Materialization.SimpleEnumerator.MoveNext() +46

Any idea what's going on here?

I even tried calling ToArray() before foreach, but that did not help.

EDIT:

If I change the query to:

    var children = DataContext.Entities.Nodes
                    .Where(n => n.Parent.Name == node.Key)
                    .ToArray()
                    .OrderBy(n => n.SortOrder);

It works... why?

EDIT 2: BTW the Parent navigator points to the same table, so each Node can have {0..1} parent Node.

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

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

发布评论

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

评论(1

心意如水 2024-11-07 17:52:05

编辑部分中的查询有效的事实表明问题出在 OrderBy(n => n.SortOrder) 子句中,因为只有查询的这一部分

DataContext.Entities.Nodes
                    .Where(n => n.Parent.Name == node.Key)

......实际上是在服务器上执行的。通过调用 .ToArray() 可以强制执行查询并将(未排序的)列表加载到内存中。以下 OrderBy 定义了此列表上的查询(它是 IEnumerable 而不再是 IQueryable)。然后,第二个查询将在此列表上在内存中执行,并且 EF 或 SqlCe 不参与此排序。

但一般来说,SqlCe 支持 OrderBy,所以问题仍然是为什么第一个查询会抛出异常。

您尝试排序的 Node.SortOrder 类型是什么?例如,它是否可以为空或 SqlCe 无法排序的某些“外来”类型?

The fact that the query in your Edit section works indicates that the problem is in the OrderBy(n => n.SortOrder) clause, because only this part of the query ...

DataContext.Entities.Nodes
                    .Where(n => n.Parent.Name == node.Key)

... is actually executed on the server. By calling .ToArray() you force the query to be executed and an (unsorted) list is loaded into memory. The following OrderBy defines a query on this list (which is an IEnumerable and not an IQueryable anymore). This second query will then be executed in memory on this list and EF or SqlCe is not involved in this sorting.

But generally SqlCe supports OrderBy, so the question remains why the first query throws an exception.

What type is Node.SortOrder you are trying to sort by? Is it nullable for instance or some "exotic" type which SqlCe is not able to sort by?

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