Provide extensibility to apply TagWith on internal EntityFinder queries (e.g., EntityEntry.GetDatabaseValues)
What problem are you trying to solve?
We have a use-case where, by framework convention, every database query is required to include a TagWith() comment for traceability (e.g., debugging, profiling, audit trail).
This is easy for user-defined queries (dbContext.Set
Further technical details
- EF Core version: 9.0.4
- Provider: SQL Server
- .NET 8.0
Describe the solution you'd like
It would be helpful if EF Core provided an official extensibility point to influence system queries like these.
For example:
Make the creation of the _queryRoot query overridable or having protected instead of private _queryRoot, e.g., via a virtual method in EntityFinder
Add a parameter to methods like GetDatabaseValues() to specify a tag string (similar to TagWith).
I think, Interceptors are exactly what you're looking for.
Hello, interceptors will not do.
DbCommandInterceptor can be used to mutate DbCommand itself. However, this is not the feature requested.
We enforce policy that requires all developers to include either TagWith or TagWithCallSite in each and every query. This enables to track from which file the query originates. In large systems that perform vast quentities of diverse queries, there is no easy way to connect SQL query back to it's LINQ counterpart.
Through IDbContextOptionsExtension we have implemented validator, that checks each and every query during it's compilation whether peformance debugging tags are present.
However, built-in queries pose another challenge, as the IEntityFinder does not allow to inject any tags at all, and we were not able to easily determine that the query present is in fact IEntityFinder query and should not be validated.
The only solution was to replace IEntityFinder with our own implementation that is bacisally carbon copy of the original.
The proposal is to make method that creates query root in EntityFinder protected virtual rather than private to allow mutating the root before it is used to build a query.
Otherwise, the question if how to detect, that the query being compiled is in fact sourced from IEntityFinder so it can be skipped during it's validation?