How to detect when EF Core must do some of the IQueryable operations in memory

自作多情 提交于 2019-12-01 22:16:47

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 in Startup.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));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!