Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn EF Core warnings about locally evaluated expressions to errors?

Is it possible to turn Entity Framework Core warnings about locally evaluated expressions into errors? I'd like to force myself to always write properly evaluated queries.

Microsoft.EntityFrameworkCore.Query:Warning: The LINQ expression '(...)' could not be translated and will be evaluated locally.

like image 970
Prolog Avatar asked Aug 31 '19 11:08

Prolog


1 Answers

I'd like to force myself to always write properly evaluated queries.

Sounds like a good idea. Moreover the client evaluation idea has been considered a mistake and will be removed in EF Core 3.0+, so it's good to be prepared :)

To get the desired behavior in pre 3.0, you should use the ConfigureWarnings extension method to change the default action from Log to Throw, as explained in the Optional behavior: throw an exception for client evaluation documentation topic:

optionsBuilder.ConfigureWarnings(warnings => warnings
    .Throw(RelationalEventId.QueryClientEvaluationWarning)
);

Additionally, it would be good to do the same for Ignored includes, which are another source of unexpected problems:

.Throw(RelationalEventId.QueryClientEvaluationWarning)
.Throw(CoreEventId.IncludeIgnoredWarning)
like image 126
Ivan Stoev Avatar answered Nov 06 '22 08:11

Ivan Stoev