EFCore 5.0 Interceptor statement type

Viewed 188

I've implemeted an DbCommandInterceptor adding a max recursion depth to the generated 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);
}

When not checking if the SQL starts with SELECT (command.CommandText.StartsWith("SELECT")) I sure get an exception on update statements (e.g. context.SaveChanges()). Is there a neater solution for checking the query type? Like an enum on the Command or similar?

Thank you!

1 Answers
Related