0

I'm trying to make odata rest-based service calls through a URI.

I have an ADO.NET Entity data model that is mapped to one of the tables on the SQL back end. Which created the necessary edmx file under my "Models" folder

I then created a controller labeled spcontrol using the "Web API 2 Odata Controller with actions, using Entity Frameowrk" option. I then set the model class and Data context class and the visual studio scaffolding did the rest. The first few lines of the controller look like this...

public class spcontrolController : ODataController
{
    private Entities db = new Entities();

    // GET odata/spcontrol
    [Queryable]
    public IQueryable<Database_table_Name> Getspcontrol()
    {
        return db.Database_table_Name;
    }

    // GET odata/spcontrol(5)
    [Queryable]
    public SingleResult<Database_table_Name> GetDatabase_table_Name([FromODataUri] int key)
    {
        return SingleResult.Create(db.Database_table_Name.Where(Database_table_Name=> Database_table_Name.ID == key));
    }

and then it goes on for the PUT, POST, PATCH, DELETE methods. In my WebApiConfig.cs file, I set the following lines

using System.Web.Http.OData.Builder;
using resttest.Models;
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Database_table_Name>("spcontrol");
config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());

Finally, I'm trying to query the table with odata. So my url looks something along the lines of

 http://testsite:8012/odata/spcontrol?$top=5

However, this call does not return any data, but only returns the following text

{
  "odata.metadata":"http://localhost:33421/odata/$metadata#spcontrol","value":[
 ]
}

How can I get the actual values within the SQL table? What am I doing wrong?

prawn
  • 2,643
  • 2
  • 33
  • 49

1 Answers1

0

Looks like it is working fine - you just have no data to return so array in response payload is empty. Add some data and hit the endpoint again.

Run a profiler on your database to see what commands are being sent from your service to check correct tables are being used etc

eoghank
  • 1,013
  • 7
  • 10
  • Interesting. I tried it on a different table and it seemed to work. I was able to get values back. I didn't run a profiler just yet because then I would have to get the DBA involved. Any ideas why using it on that one table wasn't yielding any results? – prawn Mar 28 '14 at 15:56
  • If you don't have access to the database to profile then you could print the EF SQL statements: http://stackoverflow.com/questions/7901814/debugging-entity-framework-sql-statements – eoghank Mar 28 '14 at 16:17