Skip to content

Instantly share code, notes, and snippets.

@manoj-choudhari-git
Created July 17, 2021 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-choudhari-git/7c31d4388984d2d9c85501dc76554cd0 to your computer and use it in GitHub Desktop.
Save manoj-choudhari-git/7c31d4388984d2d9c85501dc76554cd0 to your computer and use it in GitHub Desktop.
.NET - A demo DbCommandInterceptor to add order by descending clause to the EF Core query
public class DemoDbCommandInterceptor : DbCommandInterceptor
{
public override InterceptionResult<DbDataReader> ReaderExecuting(
DbCommand command,
CommandEventData eventData,
InterceptionResult<DbDataReader> result)
{
ModifyCommand(command);
return result;
}
public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(
DbCommand command,
CommandEventData eventData,
InterceptionResult<DbDataReader> result,
CancellationToken cancellationToken = default)
{
ModifyCommand(command);
return new ValueTask<InterceptionResult<DbDataReader>>(result);
}
private static void ModifyCommand(DbCommand command)
{
if (command.CommandText.StartsWith("-- Apply OrderBy DESC", StringComparison.Ordinal))
{
Console.WriteLine("DemoDbCommandInterceptor: Applying OrderBy DESC");
command.CommandText += " Order By 1 DESC";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment