SQL Server Code gen OData server-side code from Entity Framework context?

yr9zkbsy  于 2023-08-02  发布在  其他
关注(0)|答案(2)|浏览(134)

I have a model and a database context that I scaffolded from SQL Server using Entity Framework. I would now like to expose this entire model via OData. Is this possible? If so, how do I do it? Alternatively, can I expose SQL Server data via OData directly?

It's very easy to expose one entity at a time, building the controller, etc., but it seems to me there should be a way to do it for the whole context at once. My model has dozens of tables - I don't want to have to hand-roll each one!

Also, I didn't have to do that for the old "ADO.NET Data Services", which this project is meant to replace. I'd like to think modern tools would be even better, not a step back. :)

4nkexdtk

4nkexdtk1#

It is totally possible and quite trivial, though I would strongly suggest NOT exposing Entity Framework classes directly but using a separat emodel and using i.e. automapper (mostly automapper) to map the results - that way you are not bound to the definition and naming of the entity framework classes, which i.e. may include a lot of internal fields.
It's very easy to expose one entity at a time, building the controller, etc., but it seems to me there should be a way to do it for the whole context at once

Not really - because that makes no sense in a lot of scenarios, i.e. you assume you want to expose all the data. Let me specify that for QUERYING that is basically a copy/paste operation per class and the mappings for automapper, if you use it.

My model has dozens of table

This is a SMALLER model, as far as enterprise models go.

There is no proper code generator that i know of - or would use because I have some base classes that do a lot of additional processing and evaluating (i.e. query cost - expansions can kill your server unless you limit the amounts done).
I'd like to think modern tools would be even better, not a step back. :)

There are a lot of people that do not believe in odata - mostly that is the result of a tremendous lack of real knowledge. The result of that - and a focus on the way worse services like RESTier which basically is "primitive odata without the tooling and even basic services like proper filtering and expansion" plus GRPC (which is even funnier - small spec because hey, a lot is just not even covered, so yeah, if you do not specify a query language that part is not in the spec) mean that the focus is on less toolable stuff. Given that most people do not even know that Odata can be properly accessed and queried from Excel, i.e. - well, that is what you get.

But again, I would not, never ever, expose EF classes directly in Odata - I always run them in at least 3 projects: A Data model with the repositor, an ApiModel project with the classes for the OData layer and the API project with the Automapper classes. Given that I have 3 base classes (depending on whether an entity is Code keyed (string), int keyed or guid keyed - that is just a simple copy operation, define in model etc. - yes, it is a little nasty, but you get a LOT more flexibility.

sz81bmfz

sz81bmfz2#

I would now like to expose this entire model via OData. Is this possible?

RESTier can do that.

Restier is the spiritual successor to WCF Data Services. Instead of generating endless boilerplate code with the current Web API + OData toolchain, RESTier helps you boostrap a standardized, queryable HTTP-based REST interface in literally minutes. And that's just the beginning.

Like WCF Data Services before it, Restier provides simple and straightforward ways to shape queries and intercept submissions before and after they hit the database. And like Web API + OData, you still have the flexibility to add your own custom queries and actions with techniques you're already familiar with.

https://github.com/OData/RESTier

相关问题