12

I am writing a .net Core 3.1 application and have recently updated it to Entity Framework Core 5.0. Running the application has since started showing warnings as follows:

The query uses a row limiting operator ('Skip'/'Take') without an 'OrderBy' operator. This may lead to unpredictable results.

I have reviewed every instance of my code that would use .Skip or .Take or both and they all have a OrderBy clause.

My question is, are there any flags I can set with

DbContextOptiosnBuilder() .ConfigureWarnings(w => w.Throw(RelationalEventId.???))

to help determine where these .Skip and .Take queries are being ran or any way to trigger a stack trace with this warning to narrow down the cause.

Alternatively, how can one go about silencing this warning from the console?

3
  • 2
    Is the OrderBy before or after the Skip/Take? Dec 15, 2020 at 19:25
  • OrderBy is before Dec 16, 2020 at 20:05
  • 1
    the eventd I was looking for was:CoreEventId.RowLimitingOperationWithoutOrderByWarning Dec 16, 2020 at 21:11

3 Answers 3

12

In fact, you get this warning EVEN if you use a FirstOrDefault (or SingleOrDefault) on a primary key selection i.e.

context.dbsetXXX.SingleOrDefaultAsync(xxx => xxx.ID == id);

It seems to be due in how this query in badly mapped in T-SQL - using a TAKE(1) statement. So for now I would just Ignore the warning ( using .Ignore(CoreEventId.RowLimitingOperationWithoutOrderByWarning) ...)

2
6

The EventId value you are looking for is CoreEventId.RowLimitingOperationWithoutOrderByWarning.

A query uses a row limiting operation (Skip/Take) without OrderBy which may lead to unpredictable results.

This event is in the DbLoggerCategory.Query category.

As usual, the default action is Log, and you can turn it to error

.Throw(CoreEventId.RowLimitingOperationWithoutOrderByWarning)

or suppress it

.Ignore(CoreEventId.RowLimitingOperationWithoutOrderByWarning)
5
  • I will give this a shot and see if it gets triggered. I am starting to suspect it may be a warning from another library. Dec 16, 2020 at 20:05
  • @JordanBrobyn Another library, seriously? The exception message exactly matches the explanation from the documentation (included in the answer for your and future readers convenience).
    – Ivan Stoev
    Dec 16, 2020 at 20:08
  • I mean that a library I am using may be using some Linq query that uses.Take.Skip without OrderBy that I do not have control of. Dec 16, 2020 at 20:45
  • Thank you very much for your suggestion, I was able to determine where the warning was being thrown. Even though the error mentions, .Skip and.Take, performing a query like this will also trigger this warning: _dbContext.Users.Include(r => r.Roles).Where(u => u.UserName == username).FirstOrDefault() Dec 16, 2020 at 21:07
  • 1
    Hmm, interesting, by documentation there is another one specifically for First/FirstOrDefault called CoreEventId.FirstWithoutOrderByAndFilterWarning. But I see they've been obsoleted in EFC3 and un-obsoleted in EFC5. so it's a (typical for EFC) mess.
    – Ivan Stoev
    Dec 16, 2020 at 21:18
0

Keep in mind that the OrderBy/OrderByDescending methods should be invoked before Skip/Take so that the results are not already paginated

1
  • OP already stated that he is already using OrderBy/OrderByDescending at every Skip/Take
    – Allie
    Jun 22, 2022 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.