如何检测EF Core何时必须执行内存中的某些IQueryable操作 [英] How to detect when EF Core must do some of the IQueryable operations in memory

查看:128
本文介绍了如何检测EF Core何时必须执行内存中的某些IQueryable操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经遍历了我的应用程序,有时只有 IQueryable 的一部分实际上被翻译成SQL查询,其余的工作都完成了

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.

我了解EF团队无法解决开发人员可能想出的每一种可能的表达式,并将其神奇地转化为可用的SQL查询,但是IIRC,如果EF无法将 IQueryable 中定义的所有操作都转换为SQL,则会抛出异常。

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.

是否有办法让EF Core也抛出异常,或者至少在无法将 IQueryable 完全转换为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?

推荐答案


有没有办法让EF Core也抛出异常,或者至少在无法完全将 IQueryable 转换为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?



<好的。首先,这是EF Core概念,称为客户评估,在EF Core之前版本(EF6.x)中不存在。 客户端与服务器评估文档对此进行了介绍。主题,然后禁用客户端评估部分介绍了默认行为及其更改方式:

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:


默认情况下,执行客户端评估时,EF Core将记录警告。有关查看日志记录输出的更多信息,请参见日志记录。您可以在发生客户评估时将行为更改为抛出或不执行任何操作。这是在为上下文设置选项时完成的-通常在 DbContext.OnConfiguring Startup.cs 中您正在使用ASP.NET Core。

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.

稍后可通过使用 c > DbContextOptionsBuilder 类,用于 RelationalEventId.QueryClientEvaluationWarning 。有效操作为日志(默认),忽略投掷 (所需):

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));
}

这篇关于如何检测EF Core何时必须执行内存中的某些IQueryable操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆