I've been going through my application, and there are times where only part of the IQueryable
is actually translated into a SQL query and the rest of the work is done in-memory.
I understand that there's no way for the EF team to account for every possible expression that a developer may come up with and magically translate that into a useable SQL query, but IIRC, EF would throw an exception if it was unable to translate ALL of the operations defined in an IQueryable
to SQL.
Is there a way to have EF Core also throw an exception, or at the very least, raise an event when it's unable to fully translate an IQueryable
into SQL?
Is there a way to have EF Core also throw an exception, or at the very least, raise an event when it's unable to fully translate an
IQueryable
into SQL?
Sure. First, this is a EF Core concept called client evaluation which didn't exist in pre EF Core (EF6.x). It's covered by the Client vs. Server Evaluation documentation topic, and Disabling client evaluation section explains the default behavior and how it can be changed:
By default, EF Core will log a warning when client evaluation is performed. See Logging for more information on viewing logging output. You can change the behavior when client evaluation occurs to either throw or do nothing. This is done when setting up the options for your context - typically in
DbContext.OnConfiguring
, or inStartup.cs
if you are using ASP.NET Core.
The later is achieved by using ConfigureWarnings
method of DbContextOptionsBuilder
class for RelationalEventId.QueryClientEvaluationWarning
. Valid actions are Log
(default), Ignore
and Throw
(desired):
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// ...
optionsBuilder.ConfigureWarnings(warnings =>
warnings.Throw(RelationalEventId.QueryClientEvaluationWarning));
}
来源:https://stackoverflow.com/questions/47682200/how-to-detect-when-ef-core-must-do-some-of-the-iqueryable-operations-in-memory