Skip to content

Interception: raw query vs. LINQ #22899

Closed as not planned
Closed as not planned

Description

When detecting the execution of queries using interceptors or diagnostics listeners, is it possible to distinguish between a raw query (ExecuteSqlRawAsync()) vs. a regular LINQ one?

I'm thinking somewhat analog to ChangeTracker.Tracked, whose EntityTrackedEventArgs contains a FromQuery boolean indicating whether it was a query that caused the tracking.

I have looked at the events coming through the diagnostics listener. Initially it seemed I could distinguish LINQ queries by the presence of Microsoft.EntityFrameworkCore.Query.QueryCompilationStarting or Microsoft.EntityFrameworkCore.Query.QueryExecutionPlanned. Alas, these only seem to happen the first time a query is performed.

Why is it relevant to detect raw queries? Well, besides variants of SaveChanges (which can be easily intercepted), raw queries seem to be all that can have Entity Framework perform write operations (but correct me if I'm wrong). For example, if we want to begin an explicit transaction before any write operations take place, then to be safe we would need to do so before executing a raw query as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions