How to use OData in Asp.net Core Web API without Entity Model?

Viewed 1538

I am using OData in Asp.net Core web API. I am new to OData.

I used the Odata like this:

Controller

 [EnableQuery]
 public async Task<ActionResult> ListAsync([FromODataUri]ODataQueryOptions<Orders> opts)
 {
   .........
 }`

Model

 public List<Orders> GetList(ODataQueryOptions<Orders> opts)
 {
     if (order.Count == 0) 
        BindDataSource(); 
     var results = order.AsQueryable(); 
     var count = results.Count(); 
     if (opts.OrderBy != null) 
        results = opts.OrderBy.ApplyTo(results); 
     if (opts.Filter != null) 
     { 
        if (opts.Filter.RawValue.Contains("substring")) 
        { 
            string key = opts.Filter.RawValue.Split(new string[] { "'" }, StringSplitOptions.None)[1]; 
            results = results.Where(fil => fil.CustomerID.Contains(key) || fil.EmployeeID.ToString().Contains(key) || fil.Freight.ToString().Contains(key) || fil.OrderID.ToString().Contains(key) || fil.ShipCity.Contains(key) || fil.OrderDate.ToString().Contains(key)); 
        } 
        else 
            results = opts.Filter.ApplyTo(results, new ODataQuerySettings()).Cast<Orders>(); 
        }
     }
     if (opts.InlineCount != null) 
        count = results.Count(); 
     if (opts.Skip != null) 
        results = opts.Skip.ApplyTo(results, new ODataQuerySettings()); 
     if (opts.Top != null) 
        results = opts.Top.ApplyTo(results, new ODataQuerySettings());
 }

Note

I no need to create EDM for using OData.

https://damienbod.com/2018/10/12/odata-with-asp-net-core/

Here, it is integrated with entity framework. But I no need to integrate. I need to use it's attribute only.

How to use it?

When I am running the API, I am getting an error like

InvalidOperationException: No service for type 'Microsoft.AspNet.OData.ODataOptions' has been registered.

Note

I need to use OData in web API without EDM.

0 Answers
Related