gpt4 book ai didi

c# - 如何检测 EF Core 何时必须在内存中执行某些 IQueryable 操作

转载 作者:行者123 更新时间:2023-11-30 15:55:57 25 4
gpt4 key购买 nike

我一直在检查我的应用程序,有时只有部分 IQueryable 实际上被转换为 SQL 查询,其余工作在内存中完成。

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

有没有办法让 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) 之前的版本中不存在。它包含在 Client vs. Server Evaluation 中文档主题和禁用客户端评估部分解释了默认行为以及如何更改它:

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.

后者是通过使用ConfigureWarnings实现的DbContextOptionsBuilder的方法RelationalEventId.QueryClientEvaluationWarning 类。有效操作是 Log(默认)、IgnoreThrow(期望):

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// ...
optionsBuilder.ConfigureWarnings(warnings =>
warnings.Throw(RelationalEventId.QueryClientEvaluationWarning));
}

关于c# - 如何检测 EF Core 何时必须在内存中执行某些 IQueryable 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47682200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com