Dexter
Dexter

Reputation: 186

Use Entity Framework to access data from a Web service?

So I've used Entity Framework as a data abstraction layer on SQL databases, avoiding the need to write SQL queries, but using (something like?) LINQ instead. And then EF translates all that to SQL and connects to databases automatically. It's a very easy way to manage data given a model.

Now I am making a REST client, and I am thinking of using EF for accessing that as well.

So how do I make this thing connect to a REST API, instead of an SQL database? Googling Entity Framework together with REST only told me how to make a REST server use EF for accessing a database. I want to use EF on a the client side that accesses a Web server, that in turn accesses the data.

So I expect something like writing model.Users.Where(...) and it being translated to a GET /api/users?filter... HTTP request (instead of classic SELECT * FROM Users WHERE... SQL query).

I tried creating an "ADO.NET Entity Data Model" like I do usually but all options it provides require me to specify a connection string - this only applies to databases. I want an API endpoint, not an SQL connection string.

AFAIK EF connects to a "DataSet" object (created automatically and connected to a database), but then again I don't know how to make those use REST API. Do I need to write my own class that implements DataSet, and somehow make EF use THAT? Everything I've seen about DataSets also seem to literally imply SQL databases. Grrr.

Sorry for long post with such a simple question. So does someone know how to make Entity Framework use a REST service instead of a database? It should be so simple. After all, Microsoft calls it a data access abstraction layer. Not database access.

Upvotes: 4

Views: 2748

Answers (1)

ilya korover
ilya korover

Reputation: 230

I think the best start to you is to look on the OData protocol that has out of the box support by ASP.NET Web API. Check that link for good tutorials that should help you.

Upvotes: 2

Related Questions