Click here to Skip to main content
14,102,589 members
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 9 Aug 2013
Licenced CPOL

Using C#, MVC, WebAPI, OData, Entity Framework, DI/IoC, and Kendo UI MVC and Efficient Data Retrieval

, 13 Aug 2013
Rate this:
Please Sign up or sign in to vote.
WebAPI, OData, Entity Framework (EF DB First), MVC


The background for this quick article is based on how to properly pass along OData query parameters through to Entity Framework.

To setup your Entity Framework Database First project, I recommend familiarizing yourself with the content in the following links:

Once you have your Entity Framework project created, in an MVC project, you can create the following controller action method to access data using OData.

Next you need to install the following into your MVC project through Nuget:

Once these have been installed, I suggest reviewing OData Using C#, MVC, WebAPI, Entity Framework, Dependency Injection (DI)/Inversion of Control (IoC) and Kendo UI.  

Direct Entity Framework Access with OData 

If you wish to pass along OData query options for Entity Framework to process (recommended), the following code will allow you to do this:

/// <summary>
/// Example of using the EF db context directly
/// Since we want manual control over applying query options
///   it is not recommended to use the QueryableAttribute:
///     [Queryable(AllowedQueryOptions = AllowedQueryOptions.All, PageSize = 10)]
/// This will apply the query a second time - to the output - not what we want.
/// Use this option if not applying the query options - using options.ApplyTo(...)
/// </summary>
/// <param name="options"></param>
/// <returns></returns>
public IEnumerable<USER> Get(ODataQueryOptions<USER> options)
    var dbContext = new ATMS.DAL.AtmsContext();
    var ret = options.ApplyTo(dbContext.USERS).Cast<USER>().ToArray();
    // applies filtering options

    //var ret = dbContext.USERS.ToArray(); 
    // returns all records, filtered results to client

    //var ret = options.ApplyTo(dbContext.USERS).Cast<USER>() as IQueryable<USER>;        

    return ret;

Note that we are returning IEnumerable and not IQueryable.

Take note, that when using a DI/IoC container, code refactoring has to be done to your Entity Framework project.  Details discussed in the link above.  

Points of Interest

Just because you see a filtered data set on the client, doesn't mean that the entire transport process has been efficient.  You need to ensure that you are returning the optimal amount of data from your database, rather than large inflated data sets and filtering later.  There are a few "gotcha's" that this article identifies.

For a more detailed way of accomplishing this and how to refactor your Entity Framework project for access through a generic repository, see: OData Using C#, MVC, WebAPI, Entity Framework, Dependency Injection (DI)/Inversion of Control (IoC) and Kendo UI.     


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Systems Engineer
United States United States
I am a technical architect/senior software engineer, technical trainer, entrepreneur.

I have worked in several sectors from healthcare, to entertainment and global intelligent traffic systems, using .Net, SQL, NoSQL and some of the latest technologies to deliver quality software systems for clients.

Past tech flavors include C#, WCF, SOA, MVC, MVVM, Silverlight, Assembler, Pascal, VB, Java/J2EE/EJB/JDBC, Perl, NLTK, TSQL, NoSQL, KendoUI, NodeJS, SignalR, Backbone JS, Angular JS, Latest .Net technologies, Amazon AWS...

You may also be interested in...


Comments and Discussions

QuestionBest practise Pin
Bernhard Marx1-Dec-16 3:54
memberBernhard Marx1-Dec-16 3:54 
GeneralMy vote of 2 Pin
kannankeril16-Dec-14 17:10
memberkannankeril16-Dec-14 17:10 
QuestionCan you post entire solution to understand big picture. Pin
GOPINATH_P22-Sep-14 18:02
memberGOPINATH_P22-Sep-14 18:02 
GeneralMy vote of 1 Pin
webbsk17-Jul-14 18:23
memberwebbsk17-Jul-14 18:23 
Questionbroken links Pin
Tariq Salah4-Jul-14 8:45
memberTariq Salah4-Jul-14 8:45 
GeneralMy vote of 1 Pin
Brian Schummer15-Jan-14 2:51
memberBrian Schummer15-Jan-14 2:51 
QuestionNice article Pin
appxdev16-Oct-13 22:55
memberappxdev16-Oct-13 22:55 
GeneralGood stuff! Pin
dave.dolan9-Aug-13 17:43
memberdave.dolan9-Aug-13 17:43 
GeneralMy vote of 5 Pin
mag139-Aug-13 9:57
membermag139-Aug-13 9:57 
GeneralRe: My vote of 5 Pin
xbadenx9-Aug-13 10:53
memberxbadenx9-Aug-13 10:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190518.1 | Last Updated 13 Aug 2013
Article Copyright 2013 by xbadenx
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid