Database Reference
In-Depth Information
Solution
Let's say that you have a model like the one shown in Figure 9-1 .
Figure 9-1. A model for orders
Our model represents Orders. We want to put the model and database code behind a Web API service so that
any client that consumes HTTP can insert, update, and delete data into orders. To create the service, perform the
following steps:
1.
Create a new ASP.NET MVC 4 Web Application project, selecting the Web API template
from the Project Templates wizard. Name the project Recipe1.Service .
Add a new Web API Controller to the project entitled OrderController .
2.
3.
As shown in Listing 9-1, add the Order entity class.
Listing 9-1. Order Entity Class
public class Order
{
public int OrderId { get; set; }
public string Product { get; set; }
public int Quantity { get; set; }
public string Status { get; set; }
public byte[] TimeStamp { get; set; }
}
Add a reference in the Recipe1.Service project to the Entity Framework 6 libraries.
Leveraging the NuGet Package Manager does this best. Right-click on Reference, and
select Manage NuGet Packages. From the Online tab, locate and install the Entity
Framework 6 package. Doing so will download, install and configure the Entity Framework
6 libraries in your project.
4.
Then add a new class entitled Recipe1Context , and add the code from Listing 9-2 to it,
ensuring that the class derives from the Entity Framework DbContext class.
5.
 
Search WWH ::




Custom Search