Why are there missing client-side results when using OData options with Entity Framework DBContext?

前端 未结 1 725
甜味超标
甜味超标 2021-01-28 11:59

OData and Entity Framework are suppose to work well together, in that OData options will be passed to the EF db context to filter queries - ie. return only the number of records

1条回答
  •  深忆病人
    2021-01-28 12:09

    You don't mix and match QueryableAttribute and ODataQueryOptions. Pick one depending on whether you want manual control over applying the query options (ODataQueryOptions) or make it happen automatically (QueryableAttribute).

    You have two options,

    public IEnumerable Get(ODataQueryOptions options)
    {
        var dbContext = new ATMS.DAL.AtmsContext();
        var ret = options.ApplyTo(dbContext.USERS).Cast();
        return ret;
    }
    

    or

    [Queryable]
    public IEnumerable Get(ODataQueryOptions options)
    {
        var dbContext = new ATMS.DAL.AtmsContext();
        var ret = dbContext.USERS;
        return ret;
    }
    

    The reason you are seeing that behavior is that you are applying the query twice, once using ODataQueryOptions.ApplyTo and then again through QueryableAttribute.

    0 讨论(0)
提交回复
热议问题