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

前端 未结 1 759
甜味超标
甜味超标 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<T>. Pick one depending on whether you want manual control over applying the query options (ODataQueryOptions<T>) or make it happen automatically (QueryableAttribute).

    You have two options,

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

    or

    [Queryable]
    public IEnumerable<USER> Get(ODataQueryOptions<USER> 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<T>.ApplyTo and then again through QueryableAttribute.

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