Last post Aug 12, 2016 01:45 AM by Yohann Lu
Aug 10, 2016 07:27 PM|kmcnet|LINK
Hello everyone and thanks for your help in advance. I am new to OData and Entity Framework and am trying to understand how to execute a raw SQL query based on the example at
https://msdn.microsoft.com/en-us/data/jj592907. Here is my code:
Public Function [Get]() As IQueryable(Of tbl_Log_ClaimHeader) '
Dim ClaimHeaders As New List(Of tbl_Log_ClaimHeader)
Using context = New tbl_Log_ClaimHeaderContext()
ClaimHeaders = context.tbl_Log_ClaimHeader.SqlQuery("SELECT * FROM dbo.tbl_Log_ClaimHeader").ToList()
However, when I try:
Returns Error 500. I've tried some different ways of serializing the response, but can't get it correct. Any help would be appreciated.
Aug 11, 2016 02:24 AM|Yohann Lu|LINK
From your description, I suggest you can try the following code.
Please debug your code to find which code causes the error. You could also share us more relevant message(you can press F12 to open the development tool) to help us reproduce the problem.
Aug 11, 2016 03:44 PM|kmcnet|LINK
Thank you for the response. Your solution worked, but could you please explain why this was needed? My understanding of OData is that it is an extension of WebApi and that data returned from the call of an OData endpoint would, by default, return JSON
data. I'm assuming that the query in my example executes ToList which is IEnumerable, but since this is a basic query against the database, I would not think a conversion to IQueryable would be needed.
Aug 12, 2016 01:45 AM|Yohann Lu|LINK
If we not Add AsQueryable() in actions; It will return all the rows in the ‘ClaimHeaders’ table of the database with no ability to filter, sort etc.
So, when you Add the [EnableQuery] attribute to your actions, you should Add AsQueryable() to your actions. In this way, you can use the full OData syntax to query sort etc without any extra code.