EFCore 5.0拦截器语句类型

EFCore 5.0 Interceptor statement type

我已经实现了一个 DbCommandInterceptor,为生成的 SQL:

添加了最大递归深度
public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result, CancellationToken cancellationToken = default)
{
    if (command.CommandText.StartsWith("SELECT") && !command.CommandText.EndsWith($" OPTION(MAXRECURSION {MaxRecursion})"))
    { command.CommandText += $" OPTION(MAXRECURSION {MaxRecursion})"; }

    return base.ReaderExecutingAsync(command, eventData, result, cancellationToken);
}

当不检查 SQL 是否以 SELECT (command.CommandText.StartsWith("SELECT")) 开头时,我肯定会在更新语句中遇到异常(例如 context.SaveChanges())。是否有更简洁的解决方案来检查查询类型?像命令上的枚举或类似的?

谢谢!

目前不支持,Github 已创建功能请求:

https://github.com/dotnet/efcore/issues/23719