Database Reference
In-Depth Information
Our model represents Travel Agents and their corresponding Bookings. 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 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 Recipe3.Service .
2.
Add a new Web API Controller to the project entitled TravelAgentController .
3.
Next, from Listing 9-12 add the TravelAgent and Booking entity classes.
Listing 9-12. Travel Agent and Booking Entity Classes
public class TravelAgent
{
public TravelAgent()
{
this.Bookings = new HashSet<Booking>();
}
public int AgentId { get; set; }
public string Name { get; set; }
public virtual ICollection<Booking> Bookings { get; set; }
}
public class Booking
{
public int BookingId { get; set; }
public int AgentId { get; set; }
public string Customer { get; set; }
public DateTime BookingDate { get; set; }
public bool Paid { get; set; }
public virtual TravelAgent TravelAgent { get; set; }
}
Add a reference in the Recipe3.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.
4.
Then add a new class entitled Recipe3Context , and add the code from Listing 9-13 to it,
ensuring that the class derives from the Entity Framework DbContext class.
5.
Listing 9-13. Context Class
public class Recipe3Context : DbContext
{
public Recipe3Context() : base("Recipe3ConnectionString") { }
public DbSet<TravelAgent> TravelAgents { get; set; }
public DbSet<Booking> Bookings { get; set; }
 
Search WWH ::




Custom Search