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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With