Created
July 17, 2021 17:07
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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