我們有一個包含一些XML字段的SQL Server視圖。 我們首先使用Entity Framework(v5)數據庫創建了一個DBContext。 我們增加了一個WCF數據服務,並將其分配到的DbContext,如:WCF Data Services with Entity Framework with View包含Xml
public class ProductsDBSvc : DataService<ProductsDBEntities>
{
config.UseVerboseErrors = true;
config.SetEntitySetPageSize("vw_Release", 100);
config.SetEntitySetAccessRule("vw_Release", EntitySetRights.AllRead);
}
使用的瀏覽器,我們做這樣的判斷http://localhost:60606/Products/vw_Release但它返回下面的錯誤:
<m:internalexception>
<m:message>
The XML data type cannot be compared or sorted, except when using the IS NULL operator.
</m:message>
<m:type>System.Data.SqlClient.SqlException</m:type>
<m:stacktrace>
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
</m:stacktrace>
</m:internalexception>
的錯誤意味着我們不能使用order by子句中的XML列,這是一個SQL Server錯誤。 儘管我們從未在查詢中詢問過訂單。
因此,我們激活了SQL事件探查器,我們發現了以下信息: 1-實體框架引用視圖(而不是表)的OData WCF服務發送一個SQL語句,其中包含像這樣的每個列的順序:
SELECT
[vw_Release].[ID] AS [ID],
[vw_Release].[TypeNameXml] AS [TypeNameXml],
[vw_Release].[StatusNameXml] AS [StatusNameXml],
[vw_Release].[Created] AS [Created],
[vw_Release].[Modified] AS [Modified],
FROM [dbo].[vw_Release] AS [vw_Release]) AS [Extent1]
) AS [Project1]
ORDER BY [Project1].[Created] ASC,
[Project1].[ID] ASC, [Project1].[Modified] ASC,
[Project1].[StatusNameXml] ASC, [Project1].[TypeNameXml] ASC
2-獲取同樣的信息,但是從表而不是一個視圖僅僅閱讀發送:
SELECT
[Release].[ID] AS [ID],
[Release].[TypeNameXml] AS [TypeNameXml],
[Release].[StatusNameXml] AS [StatusNameXml],
[Release].[Created] AS [Created],
[Release].[Modified] AS [Modified],
FROM [dbo].[Release] AS [vw_Release]) AS [Extent1]
ORDER BY [Project1].[Created] ASC,
[Extent1].[ID] ASC
所有我們所做的事情是超出一切的箱子功能,並可以很容易地轉載。是否有辦法告訴WCF數據服務不要包含所有列的排序?