Assemblies affected
ASP.NET Core OData 8.2.5
Describe the bug
I am getting error – Could not find a property named ‘keyName’ on type ‘MyClass’ on ASP.NET Core OData 8.2.5, but it is working fine till ASP.NET Core OData 8.2.4 version.
The exception is raised at line “(int)oDataQueryOptions.Count?.GetEntityCount(oDataQueryOptions.Filter?.ApplyTo(myClassQuery, new ODataQuerySettings()) ?? myClassQuery)” mentioned in below code snippet
Reproduce steps
Create a class, c# WebApi as show below:
Call the API with URL as “https://localhost:8080/api/requests?$format=json&$top=10&$orderby=keyName&$count=true”
It will give an exception.
Model
Public class MyClass
{
[Key]
public int AutoId {get;set;}
public string KeyName {get;set;}
public string Description {get;set;}
}
WebAPI
public Task GetMyClassData(ODataQueryOptions oDataQueryOptions)
{
var myClassQuery = dbContext.Table1
.GetAllAsNoTracking()
.Select(t => new MyClass
{
AutoId = t.Autoid,
KeyName = t.KeyName,
Description = t.Description,
});
var response = new
{
totalRecords = (int)oDataQueryOptions.Count?.GetEntityCount(oDataQueryOptions.Filter?.ApplyTo(myClassQuery, new ODataQuerySettings()) ?? myClassQuery),
records = oDataQueryOptions.ApplyTo(myClassQuery).Cast()
};
}
Request/Response
Request: https://localhost:8080/api/requests?$format=json&$top=10&$orderby=keyName&$count=true)
Response: Could not find a property named ‘keyName’ on type ‘MyClass’
Expected behavior
It should return valid object
Valid Object should return