Odata to Sql Query #712
rbalagangadharan
started this conversation in
General
Replies: 1 comment 5 replies
-
I am not 100% sure what is your question, but just for reference I managed to implement odata endpoints using sqlkata query builder and entity framework. See the example bellow: public class ProductsController : ODataController
{
private readonly MyDbContext _db; // You must register ProductOdataModel in your db context.
public ProductsController(MyDbContext db)
{
_db = db;
}
[EnableQuery]
public IQueryable<ProductOdataModel> Get()
{
var compiler = new SqlServerCompiler();
var query = SqlKata.Query("Products").Select("Id", "Name"); // Your custom query
var sql = compiler.Compile(query);
return _db.Set<ProductOdataModel>()
.FromSqlRaw(sql.Sql, sql.NamedBindings.Select(x => new SqlParameter(x.Key, x.Value)).ToArray()) // Returns IQueryable<ProductOdataModel>
.AsNoTracking(); // Stop tracking changes
}
...
} Please note, that |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
Is it possible to convert
ODataQueryOptionParser
orODataQueryOptions
to SQL query.If we have static tables, we can use EF core, which magically converts the OData to SQL. In our case all the tables are dynamic and can't use any ORM.
We tried using the libraries like DynamicODataToSql, but it has limitations.
So, wondering if SQLKATA supports any like this.
Beta Was this translation helpful? Give feedback.
All reactions