Translate OData queries to SQL

旧城冷巷雨未停 提交于 2019-12-10 11:15:11

问题


I'm in an ODataController and I have an ODataQueryOptions (and all the rest of it).

I know that the source of the data is a table in SQL Server.

However, my EF model (that was used by the WEB API framework to route the HTTP request to my controller) is typeless, i.e., I don't have a POCO class.

Normally -- if I did have a POCO class -- the framework would magically write SQL from the OData query so that the paging, filtering, and sorting is done on the database and not in memory on the web server. You can use Database.Log on the DbContext to see the SQL that's generated from LINQ statements involving the POCO classses.

How do I get this to happen for me? I have the EF model, I have the OData request, but how do I combine them to query SQL Server?


回答1:


I wrote a package to convert OData queries into IQueryable objects that can be used to directly query the underlying database. You still need to provide metadata about the table, but it can be passed at runtime rather than requiring a POCO.

As far as generating SQL goes, the returned IQueryable objects are EntityFramework ObjectQuery objects, so you can just call ToString() to show the SQL, but there is currently no way to retrieve the parameters and associated values, so you're just better off using the generated IQueryable to directly query the database.



来源:https://stackoverflow.com/questions/28372999/translate-odata-queries-to-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!