Search code examples
c#asp.net-core.net-coreodatawebapi

Could not find a property named 'keyName' on type 'MyClass' on ASP.NET Core OData 8.2.5


Assemblies affected: ASP.NET Core OData 8.2.5

I am getting an error

Could not find a property named 'keyName' on type 'MyClass' on ASP.NET Core OData 8.2.5

but it was working fine up to the ASP.NET Core OData 8.2.4 version.

The exception is raised on this line:

(int)oDataQueryOptions.Count?.GetEntityCount(oDataQueryOptions.Filter?.ApplyTo(myClassQuery, new ODataQuerySettings()) ?? myClassQuery)

mentioned in below code snippet.

Reproduce steps

  • Create a class, C# Web API as show below.
  • Call the API with URL https://localhost:8080/api/requests?$format=json&$top=10&$orderby=keyName&$count=true

You will get an exception.

Model

public class MyClass
{
    [Key]
    public int AutoId { get; set; }
    public string KeyName { get; set; }
    public string Description { get; set; }
}

Web API:

public Task GetMyClassData(ODataQueryOptions oDataQueryOptions)
{
    var myClassQuery = dbContext.Table1
                                .GetAll()
                                .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


Solution

  • As per Microsoft.AspNetCore.OData 8.2.5 team, they have accepted that there was a bug in the specific version and it will be resolved. For more details :BugDetails