8
votes

I'm building a WebAPI that is getting its data from legacy systems so no Entity Framework available for me. Now I want to use OData functionality but is not going to work if I have somethink like Entity Framework. But in my research I found out that I can fetch the ODataQueryOptions like this.

    public IQueryable<Vehicle> Get(ODataQueryOptions opts)
    {
        var dal = new DataAccessVehicles();

        return (dal.GetVehicles(opts));                        
    }

In my DAL I could translate the OData query to an actual SQL query. But this does seem like a lot of work.

My question is, is there another way or a better way to achieve this without using Entity Framework. Any tips/help would be appreciated.

1
Why can't you just build an Entity Framework model on top of the existing database? I'm guessing there are a lot of business rules embedded in the legacy libraries, either that or there is no EF provider for the data store..? - Charleh
@Charleh Not always possible, I had the same problem recently trying to put EF on top of an Oracle database but few of the tables had primary keys so didn't work. - DavidG
@Charleh There are indeed a lot of business rules inside the legacy code, and I'm not allowed to make any changes to existing code in the libraries. - Maurice van Lieshout

1 Answers

0
votes

Either enable query support at startup to allow OData queries for all actions, or decorate your actions with [Queryable]. Then make sure your actions return IQueryable, you can use the .AsQueryable() method on your existing collections.

Example:

[Queryable]
public IQueryable<Product> Get() 
{
    return products.AsQueryable();
}

Take a look at this article:

http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options