【问题标题】:ObjectQuery error when using STE and WCF使用 STE 和 WCF 时出现 ObjectQuery 错误
【发布时间】:2012-01-08 11:41:06
【问题描述】:

我在 EF4 和 WCF 中实现 STE 时收到此错误

“找不到标识为“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

希望有人可以指导我。如果您需要,也可以提供上述示例的完整源代码

【问题讨论】:

    标签: vb.net wcf entity-framework-4 self-tracking-entities objectquery


    【解决方案1】:

    这不适用于 WCF。您必须始终发回真实的实体类型,而不是抽象类的 DbDataRecord。 WCF 必须知道使用的真实类型是什么。 EF 中真正的类型可能是MaterializedDbRecord,它是内部的且不可序列化的。

    通过 WCF 公开方法时,您必须使用简单类型或数据协定/序列化类型作为参数和结果。 STE 是数据合同,因此请直接使用它们 - 这显然会破坏您从客户端发送无类型查询的要求,但这不是 WCF 的场景。

    如果您正在寻找可让您在客户端上创建查询的技术,请查看WCF Data Services(但它们不支持 STE)。

    【讨论】:

    • 你的意思是我们不能用 wcf 返回匿名类型?必须返回强类型?
    猜你喜欢
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多