Had a good buddy ask me a few minutes ago about exposing data entities in WebAPI. It’s quick, and almost painless…
Start with creating a new project called WebServices or the like in Visual Studio 2013. Select the WebAPI profile – we’ll only need MVC/WebAPI pieces, not webforms, for this sample.
Start with an Entity Framework data model – here I used the Purchasing.Vendor table from AdventureWorks2012. If you don’t have EF, but it’s POCO, no biggie – you can still right-mouse click on any of the entities and generate views/controllers. Build the project – EF uses Reflection so we need to run a build to work with these entities and expose them in WebAPI.
Right mouse click on the Controllers folder and select Add, New Controller.
In the next screen select the Web API 2 Controllers option.
In the following screen, select your exposed table and make sure you generate a new Data Context as below. I always use the async controller actions by default.
Visual studio will think a little, and then you’ll see the auto-generated class. Look at all those scaffolded goodies!
Run a publish, and navigate using one of those handy REST api calls documented above. For example, sending <myserver>/api/Vendor returned this:
I’m going to attach the ZIP folder of the project. So easy, it feels like stealing!!! WebAPISample
For more detail, see the post here – https://driftboatdave.com/2014/01/13/webapi-the-new-wcf-data-services-and-knockoutjsmvvm-explorations/