Introduction
In this post, we will learn about OData by using ASP.Net Web API 2 in MVC application.
What’s OData protocol?
The Open Data Protocol (OData) is a data access protocol for the web. OData provides a uniform way to query and manipulate data sets through CRUD operations (create, read, update, and delete).
Prerequisites
As I said earlier, we are going to use OData protocol in our MVC application. For this, you must have Visual Studio 2015 (.NET Framework 4.5.2) and SQL Server.
SQL Database part
Here, find the script to create database.
Create Database
- USE [master]
- GO
- /****** Object: Database [EmployeeDB] Script Date: 9/27/2016 2:58:52 AM ******/
- CREATE DATABASE [EmployeeDB]
- CONTAINMENT = NONE
- ON PRIMARY
- ( NAME = N'EmployeeDB', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\EmployeeDB.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
- LOG ON
- ( NAME = N'EmployeeDB_log', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\EmployeeDB_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
- GO
- ALTER DATABASE [EmployeeDB] SET COMPATIBILITY_LEVEL = 110
- GO
- IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
- begin
- EXEC [EmployeeDB].[dbo].[sp_fulltext_database] @action = 'enable'
- end
- GO
- ALTER DATABASE [EmployeeDB] SET ANSI_NULL_DEFAULT OFF
- GO
- ALTER DATABASE [EmployeeDB] SET ANSI_NULLS OFF
- GO
- ALTER DATABASE [EmployeeDB] SET ANSI_PADDING OFF
- GO
- ALTER DATABASE [EmployeeDB] SET ANSI_WARNINGS OFF
- GO
- ALTER DATABASE [EmployeeDB] SET ARITHABORT OFF
- GO
- ALTER DATABASE [EmployeeDB] SET AUTO_CLOSE OFF
- GO
- ALTER DATABASE [EmployeeDB] SET AUTO_CREATE_STATISTICS ON
- GO
- ALTER DATABASE [EmployeeDB] SET AUTO_SHRINK OFF
- GO
- ALTER DATABASE [EmployeeDB] SET AUTO_UPDATE_STATISTICS ON
- GO
- ALTER DATABASE [EmployeeDB] SET CURSOR_CLOSE_ON_COMMIT OFF
- GO
- ALTER DATABASE [EmployeeDB] SET CURSOR_DEFAULT GLOBAL
- GO
- ALTER DATABASE [EmployeeDB] SET CONCAT_NULL_YIELDS_NULL OFF
- GO
- ALTER DATABASE [EmployeeDB] SET NUMERIC_ROUNDABORT OFF
- GO
- ALTER DATABASE [EmployeeDB] SET QUOTED_IDENTIFIER OFF
- GO
- ALTER DATABASE [EmployeeDB] SET RECURSIVE_TRIGGERS OFF
- GO
- ALTER DATABASE [EmployeeDB] SET DISABLE_BROKER
- GO
- ALTER DATABASE [EmployeeDB] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
- GO
- ALTER DATABASE [EmployeeDB] SET DATE_CORRELATION_OPTIMIZATION OFF
- GO
- ALTER DATABASE [EmployeeDB] SET TRUSTWORTHY OFF
- GO
- ALTER DATABASE [EmployeeDB] SET ALLOW_SNAPSHOT_ISOLATION OFF
- GO
- ALTER DATABASE [EmployeeDB] SET PARAMETERIZATION SIMPLE
- GO
- ALTER DATABASE [EmployeeDB] SET READ_COMMITTED_SNAPSHOT OFF
- GO
- ALTER DATABASE [EmployeeDB] SET HONOR_BROKER_PRIORITY OFF
- GO
- ALTER DATABASE [EmployeeDB] SET RECOVERY SIMPLE
- GO
- ALTER DATABASE [EmployeeDB] SET MULTI_USER
- GO
- ALTER DATABASE [EmployeeDB] SET PAGE_VERIFY CHECKSUM
- GO
- ALTER DATABASE [EmployeeDB] SET DB_CHAINING OFF
- GO
- ALTER DATABASE [EmployeeDB] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
- GO
- ALTER DATABASE [EmployeeDB] SET TARGET_RECOVERY_TIME = 0 SECONDS
- GO
- ALTER DATABASE [EmployeeDB] SET READ_WRITE
- GO
Create your MVC application
Open Visual Studio and select File >> New Project.
The "New Project" window will pop up. Select ASP.NET Web Application (.NET Framework), name your project, and click OK.

Now, new dialog will pop up for selecting the template. We are going to choose Web API template and click OK.

Add a Model class
In Solution Explorer, right click on Models folder > Add > Class > Name your class.

Employee.cs
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations.Schema;
- using System.Linq;
- using System.Web;
- namespace WebAPIODataApp.Models
- {
- [Table("Employee")]
- public class Employee
- {
- public int EmployeeID { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string Gender { get; set; }
- public string Designation { get; set; }
- public int Salary { get; set; }
- public string City { get; set; }
- public string Country { get; set; }
- }
- }





kawtar najiPosted Jan 14, 2019, 8:48 PM
Can this approach be used instead of RDA(Remote Data Access)?
Hamid KhanPosted Nov 12, 2018, 6:05 AM
Good article
AniruddhaPosted Jan 17, 2018, 11:48 PM
Fantastico! Gracias!!!
Akshay PPosted Oct 31, 2017, 8:18 AM
Any suggestions please let me know
Akshay PPosted Oct 31, 2017, 8:17 AM
Table is not creating dynamically
Cavit OzPosted Oct 5, 2016, 5:43 AM
Thanks For Shared you. Perfect
El Mahdi ArchanePosted Sep 30, 2016, 4:58 PM
Thank you sir
Mahesh ChandPosted Sep 30, 2016, 8:32 AM
I like your writing style, EL. Thank you for following guidelines. This is a perfect way to explain your code including heading styles etc. Thanks!