Using Entity Framework in Web API Part-1

Introduction

This article explains the Entity Framework in the Web API. Here we define the Entity Framework and how to create the application with the domain models.

Entity Framework is an object oriented mapping concept that allows you to work with relational data in the form of domain specific objects or properties. It eliminates the need for most of the data access code that developers usually need to write.

Architecture of application:

Entity framework diagram

  • First the HTML pages are generated by the ASP.NET MVC.
  • Now the Web API performs the CURD operations on the data (Model classes).
  • The Entity Framework translates the C# model classes into the database entities.

Step 1

Create the projects.

  • Start Visual Studio 2013.
  • Click on  "Installed" -> "Web" -> "Visual Studio 2012".
  • From the Template window select  "ASP.NET MVC4 Web Application".
  • Click on the "OK" button.

    Select MVC4 Web Application

  • From the MVC4 project window select "Internet Application".

    Select Internet Application

Step 2

Now add the Domain Models.

  • In the Solution Explorer.
  • Right-click on the "Model" folder.

    Select Class

  • Select "Add" -> "Class".

    Add Class

  • Click on the "OK" button.

The first class is "Novel.cs". In this class add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.Linq;  
  5. using System.Web;  
  6. namespace EntityAPI.Models  
  7. {  
  8.     public class Novel  
  9.     {  
  10.         [ScaffoldColumn(false)]  
  11.         public int Id { getset; }  
  12.         [Required]  
  13.         public string Name { getset; }  
  14.         public decimal DealPrice { getset; }  
  15.         public decimal SellingPrice { getset; }  
  16.     }  
  17. }  

In the code above we use the "Scaffold Attribute" that describes that the Id of the Novel skips when the novel generates an editor form. The "Required" attribute shows the validation of the specific data.

The second class is "Item" with the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.Linq;  
  5. using System.Web;  
  6. namespace EntityAPI.Models  
  7. {  
  8.     public class Item  
  9.     {  
  10.         public int Id { getset; }  
  11.         [Required]  
  12.         public string Novel{ getset; }  
  13.         public ICollection<ItemDetail> OrderDetails { getset; }  
  14.     }  
  15. }  

The third class is "ItemDetail" with the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace EntityAPI.Models  
  6. {  
  7.     public class ItemDetail  
  8.     {  
  9.         public int Id { getset; }  
  10.         public int Quantity { getset; }  
  11.         public int ItemId { getset; }  
  12.         public int NovelId { getset; }  
  13.         public Novel Novel { getset; }  
  14.         public Item Item { getset; }  
  15.     }  
  16. }  

Step 3

Configuration of Media Type Formatters.

The Media Type Formatter works as an object that is used for serializing the data while the Web API writes the response body. There is JSON and XML supported by the builtin formatters.

Now in the "Solution Explorer" expand the "App_Start" folder and select the WebApiconfig.cs file. And write some code:

  1. var json = config.Formatters.JsonFormatter;  
  2. json.SerializerSettings.PreserveReferencesHandling =Newtonsoft.Json.PreserveReferencesHandling.Objects;  
  3. config.Formatters.Remove(config.Formatters.XmlFormatter);  

This code is used for preserving the JSON formatters and removes the XML formatters from the pipeline.