0

時,當我實現STE在EF4和WCF的ObjectQuery錯誤使用STE和WCF

我收到此錯誤「對象映射找不到與身份‘NorthwindModel.Customer’類型。」

如果我不使用STE(在單個項目中),此代碼正在工作。

Walkthrough: Serialize Self-Tracking Entities

這裏是我的代碼

WCF:

Public Class Service1 
Implements IService1 

    Public Function GetData(ByVal query As String) As List(Of DbDataRecord) Implements IService1.GetData 
    Try 
    Using ctx = New NorthwindEntities() 
     Return New ObjectQuery(Of DbDataRecord)(query, ctx).ToList 'Here is the error. 
    End Using 
    Catch ex As Exception 
    Dim theFault As New ServFault() 
    theFault.Reason = ex.Message.ToString() 
    Throw New FaultException(Of ServFault)(theFault, ex.Message.ToString) 
    End Try 
End Function 

Public Function GetOrderByCustomer(customerId As String) As System.Collections.Generic.List(Of Entities.Order) Implements IService1.GetOrderByCustomer 
    Try 
    Using ctx = New NorthwindEntities() 
     Return (From ord In ctx.Orders Where ord.CustomerID = customerId).ToList 
    End Using 
    Catch ex As Exception 
    Dim theFault As New ServFault() 
    theFault.Reason = ex.Message.ToString() 
    Throw New FaultException(Of ServFault)(theFault, ex.Message.ToString) 
    End Try 

End Function 
End Class 

WCF配置

<?xml version="1.0"?> 
<configuration> 

    <system.web> 
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 
    <connectionStrings> 
     <add name="NorthwindEntities" connectionString="metadata=res://*/NWDModel.csdl|res://*/NWDModel.ssdl|res://*/NWDModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(local);initial catalog=Northwind;persist security info=True;user id=sa;password=145837;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> 
    </connectionStrings> 
</configuration> 

WCF客戶:

Module Module1 

    Sub Main() 
     Dim srv As New Service1Client 
     Dim query As String 
     Dim lst As List(Of DbDataRecord) 

     Console.WriteLine("Start...") 

     Dim orders As List(Of Entities.Order) 
     orders = srv.GetOrderByCustomer("ALFKI") 'This code is working 
     Console.WriteLine("Success!!! Order Count: {0}", orders.Count) 

     query = "SELECT p FROM Customers AS p" 
     lst = srv.GetData(query) 

     Console.WriteLine("Total Customer: {0}", lst.Count) 

     srv.Close() 
     Console.ReadLine() 
    End Sub 

End Module 

希望有人能指導我。上述示例的完整源代碼也可在需要時使用

回答

0

這不適用於WCF。您必須始終發回真實實體類型,而不是抽象類的DbDataRecord。 WCF必須知道使用的實際類型是什麼。 EF中的真實類型可能是MaterializedDbRecord,它是內部的和不可序列化的。

當暴露在WCF方法,您必須使用簡單的類型或數據契約/ serializalbe類型作爲參數和結果。國營的數據合同,以便直接使用它們 - 這顯然傷了你的要求,從客戶端發送無類型查詢,但它不是爲情景WCF。

如果你正在尋找技術,讓您的客戶檢查WCF Data Services創建查詢(但不支持國營貿易公司)。

+0

你的意思是,我們不能與WCF返回匿名類型?必須返回一個強類型? – shoden