Sorry for the bad and irrelevant title first of all. But have you ever thought, is it really possible to expose any Odata service without Entity Framework? A few days ago I was talking with one of my fellow developers and in a word the topic raised was that we were thinking, if there is another data source other than Database or Entity Framework? How can we expose data as an Odata service?
Then I started to develop one POC to prove that yes, we can expose data without using the Entity Framework, if we think a little bit then we will get the answer ourself. We know that Odata is nothing but an endpoint or data source that allows a set of data access mechanisms to suck data from it where the underlying concept to bring data up to the endpoint is totally abstract to the end user.
So, there might be n number of ways to bring data to an endpoint but the Odata endpoint will use the uniform mechanism to expose the data with the outside world, irrespective of client and platform. So the ultimate point is that there is not a strong relationship with the Entity Framework and Odata. But when people talk about Odata/MVC and the Web API then automatically the Entity Framework is related if the underlying data source is a SQL table / database.
Fine, now let's see how to implement an Odata endpoint with our custom class. First of all create one Web API 2.0 project to the solution and enable Odata in it. You can install an Odata related package from the NuGet Package Manager.
Once it is finished create one model class to use the class as the data source. Here I have created a sample one.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace OdataAPI.Models
- {
- public class company
- {
- public int Id { get; set; }
- public string name { get; set; }
- public string location { get; set; }
- }
- public class company_rep
- {
- public static List<company> list = null;
- static company_rep()
- {
- list = new List<company>();
- list.Add(new company { Id = 1, location = "kolkata", name = "TCS" });
- list.Add(new company { Id = 1, location = "Delhi", name = "Wipro" });
- list.Add(new company { Id = 1, location = "Bangalore", name = "IBM" });
- }
- }
- }
The implementation is a very simple. company class; it is our main model class and the “company_rep” will work as the repository and it will supply the data.
Fine, now we need to register our Odata endpoint to the routing. Open the “WebApiConfig.cs ” file of the application and modify the code accordingly.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web.Http;
- using System.Web.Http.OData.Builder;
- using Microsoft.Data.Edm;
- using OdataAPI.Models;
- namespace OdataAPI
- {
- public static class WebApiConfig
- {
- private static IEdmModel GenerateEdmModel()
- {
- var builder = new ODataConventionModelBuilder();
- builder.EntitySet<company>("companyProcessing");
- return builder.GetEdmModel();
- }
- public static void Register(HttpConfiguration config)
- {
- // Web API routes
- config.MapHttpAttributeRoutes();
- config.EnableQuerySupport();
- config.Routes.MapODataRoute("odata", "odata", GenerateEdmModel());
- config.Routes.MapHttpRoute(
- name: "DefaultApi",
- routeTemplate: "api/{controller}/{id}",
- defaults: new { id = RouteParameter.Optional }
- );
- }
- }
- }
Have a look at GenerateEdmModel() function. In this function we are registering the model. Just create one Object of OdataConvertionModelBuilder class and register it.
Now, we will create one controller class where we will process our data. Have a look at the following example.






Ganesh DivekarPosted Apr 19, 2019, 4:32 AM
The example that you have given above is all in memory operations and you are not pulling anything from the database directly. When we use Entity framework and use Iqueryable objects , we make sure that we run the filtered query on the database so filtration done on the Database side and not in memory.
Sanjay Kumar PanjaguttaPosted Sep 16, 2014, 12:48 PM
Hi Sourav, Thanks for the wonderful article. I am in situation where I need to create OData service by exposing data from NoSQL database. I need to create POCO and map those to database fields. Is there any example I can find to do that. Much Appreciated, Thanks