Database Reference
In-Depth Information
// DELETE api/order/5
public HttpResponseMessage Delete(int id)
{
using (var context = new Recipe1Context())
{
var order = context.Orders.FirstOrDefault(x => x.OrderId == id);
context.Orders.Remove(order);
context.SaveChanges();
// Return Http Status code of 200, informing client that resouce removed successfully
return Request.CreateResponse(HttpStatusCode.OK);
}
}
private void Cleanup()
{
using (var context = new Recipe1Context())
{
context.Database.ExecuteSqlCommand("delete from chapter9.[order]");
}
}
}
It's important to point out that when using Entity Framework with MVC or Web API, these ASP.NET frameworks
contain a great deal of scaffolding (i.e., code generation tempates) that can generate a functioning controller that
contains Entity Framework plumbing code for you, saving you the effort of constructing it manually.
Next we create the client solution, which will consume the Web API service.
9.
Create a new Visual Studio solution that contains a Console application entitled
Recipe1.Client .
10.
Add the same Order entity class to the client that we added to the service back in Listing 9-1.
Finally, replace the code in the program.cs file with that from Listing 9-6.
Listing 9-6. Our Windows Console Application That Serves as Our Test Client
private HttpClient _client;
private Order _order;
private static void Main()
{
Task t = Run();
t.Wait();
Console.WriteLine("\nPress <enter> to continue...");
Console.ReadLine();
}
private static async Task Run()
{
// create instance of the program class
var program = new Program();
program.ServiceSetup();
 
Search WWH ::




Custom Search