Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Core 5.0 Warning limiting operator ('Skip'/'Take') without an 'OrderBy' operator

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?

like image 479
Jordan Brobyn Avatar asked Dec 15 '20 18:12

Jordan Brobyn


2 Answers

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) ...)

like image 55
flavior Avatar answered Nov 15 '22 12:11

flavior


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)
like image 23
Ivan Stoev Avatar answered Nov 15 '22 12:11

Ivan Stoev