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

asp.net-core c# entity-framework-core

Question

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.

1
3
8/31/2019 2:26:52 PM

Accepted Answer

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)
4
8/31/2019 2:26:23 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow