How to add SQL query hints to Entity Framework Core queries

I don’t think there’s anything built into Entity Framework for this as query hints can often be very DB platform specific, but if we want to send query hints along with the queries which EF generates we can do this using the interceptor system.

The steps are shown in the example below and involve..

Tagging relevant queries with some arbitrary string we’ll test for later

and

Creating an interceptor class which implements the ReaderExecuting/Asych methods (which run before a query is issued) and checks for and modifies matching queries

and

Registering the interceptor class with Entity Framework.

Click the image for a larger view in a new tab…

Note… the above example is adapted from Microsofts Entity Framework Interceptors page.

Leave a Reply

Your email address will not be published. Required fields are marked *