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:

using System;

using System.Collections.Generic;

using System.ComponentModel.DataAnnotations;

using System.Linq;

using System.Web;

 

namespace EntityAPI.Models

{

    public class Novel

    {

[ScaffoldColumn(false)]

        public int Id { get; set; }

        [Required]

        public string Name { get; set; }

        public decimal DealPrice { get; set; }

        public decimal SellingPrice { get; set; }

    }

}

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:

using System;

using System.Collections.Generic;

using System.ComponentModel.DataAnnotations;

using System.Linq;

using System.Web;

 

namespace EntityAPI.Models

{

    public class Item

    {

        public int Id { get; set; }

        [Required]

        public string Novel{ get; set; }

        public ICollection<ItemDetail> OrderDetails { get; set; }

 

    }

}

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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

 

namespace EntityAPI.Models

{

    public class ItemDetail

    {

        public int Id { get; set; }

        public int Quantity { get; set; }

        public int ItemId { get; set; }

        public int NovelId { get; set; }

        public Novel Novel { get; set; }

        public Item Item { get; set; }

 

    }

}

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:

var json = config.Formatters.JsonFormatter;

            json.SerializerSettings.PreserveReferencesHandling =

                Newtonsoft.Json.PreserveReferencesHandling.Objects;

 

            config.Formatters.Remove(config.Formatters.XmlFormatter);

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

Up Next
    Ebook Download
    View all
    Learn
    View all