I’m using OData 8.2.5 with .NET 6 and I’m trying to return results from my controller shown below. When I hit /odata/MyType it works, but if I hit /odata/MyType?$select=Id, I get the error in the title, System.NotSupportedException: Unable to create a constant value of type ‘Microsoft.OData.Edm.IEdmModel’. If I return a List and call ToList() on my set then the select works, but I can’t afford to load everything into memory before the select filters are applied. I want to make it work with IQueryable. Any help is appreciated!
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Formatter;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Results;
using Microsoft.AspNetCore.OData.Routing.Controllers;
namespace MyProject.Controllers
{
public class MyTypeController : ODataController
{
private string _apiKey = string.Empty;
public MyTypeController(IConfiguration configuration)
{
_apiKey = configuration.GetValue<string>("apikey");
}
[HttpGet]
[EnableQuery]
public IQueryable<MyTypeEntity> Get()
{
return BuildFilteredDbSet();
}
}
}
BuildFilteredDbSet.cs
public IQueryable<T> BuildFilteredDbSet()
{
return _entityDataContext.Set<T>();
}
Program.cs
builder.Services.AddControllers().AddOData(options =>
{
options.Select().Filter().OrderBy().Expand().SetMaxTop(null).Count()
.AddRouteComponents("odata", modelBuilder.GetEdmModel());
}
);