Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interception: raw query vs. LINQ #22899

Closed
Timovzl opened this issue Oct 6, 2020 · 3 comments
Closed

Interception: raw query vs. LINQ #22899

Timovzl opened this issue Oct 6, 2020 · 3 comments

Comments

@Timovzl
Copy link

Timovzl commented Oct 6, 2020

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.

@ajcvickers
Copy link
Member

@Timovzl I don't believe there is currently any way to determine this. We will consider adding it.

@Timovzl
Copy link
Author

Timovzl commented Oct 6, 2020

As a workaround, I have intercepted NonQueryExecuting and NonQueryExecutingAsync.

It's a bit fragile, though. Who knows if a scalar-returning raw SQL method will be added in the future.

And although I haven't observed other scenarios that lead to NonQueryExecuting[Async] (except initialization maybe), I'm not 100% certain.

@ajcvickers
Copy link
Member

I think this is now possible using CommandEventData.CommandSource. See #23719, implemented by #24421.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants