带有 Sql Compact Edition 4.0 的 Entity Framework 4.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑部分中的查询有效的事实表明问题出在
OrderBy(n => n.SortOrder)
子句中,因为只有查询的这一部分......实际上是在服务器上执行的。通过调用 .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 ...... 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 followingOrderBy
defines a query on this list (which is anIEnumerable
and not anIQueryable
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?