asp.net-web-apientity-framework-6asp.net-web-api-routingef-database-firstasp.net-web-api-odata

Coding my WebApiConfig.cs for a Database First oData Endpoint Service


I am building a Web API service that will generate oData. I'm using a "Database First" approach in that I've created an EDM in my "Models" folder containing a view.

screenshot of VS 2017 Solution Explorer

screenshot of my EDM

I had Visual Studio scaffold a Controller for me based on the EDM.

screenshot of VS 2017 Solution Explorer

enter image description here

Now I'm trying to figure out the routing code to put in my WebApiConfig.cs file. Every example I've found so far on the internet has involved Code First approaches. Does anyone know how to do this from a Database First approach?


Solution

  • I found where I went wrong.

    First of all, I added the wrong scaffolded controller. Originally when I added my controller, I went to (right-click "Controllers") --> Add --> New Scaffolded Item, as seen below. It gave me 2 possible Web API Controller options. I picked the one that would come pre-filled with CRUD functionality based on Entity Framework.

    enter image description here

    enter image description here

    Turns out, what I should have done was go to (right-click "Controllers") --> Add --> Controller, as seen below. From there I could add an ODATA controller!

    enter image description here

    enter image description here

    Icing on the cake? At the top of the controller, commented out, was the very code I needed for the WebApiConfig.cs!

    enter image description here

    Now, fair warning, when you run your code, it will show an error at first.

    enter image description here

    Don't freak out yet; just amend your url from "http://localhost:(insert port here)/" to "http://localhost:(insert port here)/odata/(insert model object here)"

    For example: I changed my url to "http://localhost:51044/odata/vw_FilesToBeProcessed_Dashboard" and I got my odata output!

    enter image description here