Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
826 views
in Technique[技术] by (71.8m points)

asp.net web api - Entity framework with OData(Web API) is sending Order By clause By default to Sql Query

I am using Web Api with OData. and I have an entity defined in an EF 5.0.
I am sending very simple request to Controller::

 $.ajax({url: "/odata/Details?$top=10",
            type: "GET",
            dataType: 'json',
            success: function (data) {
               viewModel.list(data.value);
            }

Now code on My controller::

 [Queryable]
    public override IQueryable<Area> Get()
    {  
    return db.Area.AsQueryable();
    }

Query i see using SQL Profiler::

 SELECT TOP (@p__linq__1) 
[Project1].[id] AS [id1], 
[Project1].[name] AS [name1], 
[Project1].[pucrhase] AS [pucrhase1], 
[Project1].[sale] AS [sale1]
FROM Area
ORDER BY [Project1].[id] DESC, [Project1].[name] ASC, [Project1].[pucrhase] ASC,      
[Project1].[sale] ASC,N',@p__linq__1 int,@p__linq__1=10

I have not requested for any Ordering , Order By Clause . EF adds ORDER BY clause by Itself to Query. Order By clause added contains all columns of table.This table has 3 millions records and Query is timing Out as it is Ordering by All columns .

I tested by removing Order By it took Less then a second to finish

So Question is

how to Stop Entity framework(support of Web Api Odata ) from sending Order By clause to Sql Query.

How to remove Order By clause from SQL Query Entity framework(Web Api Odata) runs on Server?

Any Help is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Im not sure if this is the right answer, but im assuming that the odata service is attempting to maintain a stable sort ordering by ordering on all properties within your model.

Therefore try

[Queryable(EnsureStableOrdering=false)]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...