I received this error when i implement STE in EF4 and WCF
"Object mapping could not be found for Type with identity 'NorthwindModel.Customer'."
if I'm not using STE (in single project), this code is working.
Walkthrough: Serialize Self-Tracking Entities
Here is my code
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 Config
<?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="data source=(local);initial catalog=Northwind;persist security info=True;user id=sa;password=145837;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
WCF Client:
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
Hope somebody can guide me. Full source for above sample also available if you need it
This will not work over WCF. You must always send real entity type back, not the
DbDataRecord
which is abstract class. WCF must know what is the real type used. The real type in EF is probablyMaterializedDbRecord
which is internal and non-serializable.When exposing methods over WCF you must use simple types or data contracts / serializalbe types as parameters and results. STEs are data contracts so use them directly - that obviously breaks your requirement to send untyped queries from the client but it is not scenario for WCF.
If you are looking for technology which will allow you creating queries on the client check WCF Data Services (but they don't support STEs).