WebAPI/KO App (1)

This exercise is based this page:
http://www.asp.net/web-api/overview/creating-web-apis/using-web-api-with-entity-framework/using-web-api-with-entity-framework,-part-1

Overview and creating the Project
This tutorial shows how to use WEBAPI and ADO.NET Entity framework using code-first model. UI using Knockout.
Goal: simple store application. Normal user can view products and create orders; admin users can create, delete or edit products.
image
How domain objects are represented in various layers
image

(1) create a new MVC4 internet application “ProductStore”.
(2) Update the /Content/site.css per the exercise.
Creating Domain Models
(3) Define 3 models:
image image
imageimage
(4) Build the solution so EF can discover the new models.
(5) Add to WebApiConfig.cs so we can resolve circular reference issue in JSON and also remove XMLFormatter (so JSON is the default formatter).
image
Creating an Admin Controller
(6) We add an API controller that supports CRUD operations on Product
Add a new controller (AdminController) image
(7) A OrdersContext.cs is created under /Models.
image
(8) Update the web.config so the data context points to a SQL server, not SQL CE or SQL Express.
(9) On Orders.Context.cs: this name refers to the connection string name:
image
(10) In OrdersContext.cs, add these three lines after the constructor so EF can query them:
image 
AdminController defines these 5 methods:
image
(11) Add database initializer – to seed the DB.
Right click /Models, add a new class OrdersContextInitializer.cs.
image
Then in global.asax’s Application_Start, add this line to initialize the database every time the application starts.
image
(12) Now when you do Control + F5, and
http://localhost:55784/api/admin you get this JSON result:
image
because in AdminController, the default get method returns all products as an enumerable:
image
00:50am, 01/23/2013

Next:
http://www.asp.net/web-api/overview/creating-web-apis/using-web-api-with-entity-framework/using-web-api-with-entity-framework,-part-4

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s