You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ToQueryString() and ToDbCommand() work on IQueryable, and so cannot be used when e.g. a reducing terminating operator is used (Max(), Min()); it's possible to chop it off, but that changes the SQL. The same limitation applies to ExecuteUpdate/Delete, which behave like other reducing terminating operator.
A solution to this could be ToQueryString()/ToDbCommand() overloads on DbContext, which would accept the entire query as an argument:
_= context.ToQueryString(ctx => ctx.Blogs.Max());
Note the similarity with the Query() API we've discussed several times in the past, which also accepts a full query as an argument and executes it (i.e. to get all the top-level operators as a quotation rather than execution them).
Note that this would add API noise to the DbContext surface which likely only few people will actually need. Maybe there's some way to combine ToQueryString() with the aforementioned Query() API, to help out with this.
In the meantime if anyone is looking for a workaround, I managed to make it work with a small amount of code.
Basically an interceptor records commands and suppress the execution. The collected commands are then returned to the user.
It seems to work quite well. It assumes that the sql server connector is used. Also, it's using a pre-release of Microsoft.Data.SqlClient to issue batch commands.
There are probably some gotchas that I haven't considered but it seems to work well and the performance seems quite good.
That illustrates the power of interceptors!
This should probably have the same shape as #10879 (whatever that turns out to be) and differ only in the return type i.e. string or DbBatchCommand instead of the actual result. Just like ToQueryString/CreateDbCommand vs ToList.
ToQueryString() and ToDbCommand() work on IQueryable, and so cannot be used when e.g. a reducing terminating operator is used (Max(), Min()); it's possible to chop it off, but that changes the SQL. The same limitation applies to ExecuteUpdate/Delete, which behave like other reducing terminating operator.
A solution to this could be ToQueryString()/ToDbCommand() overloads on DbContext, which would accept the entire query as an argument:
Note the similarity with the Query() API we've discussed several times in the past, which also accepts a full query as an argument and executes it (i.e. to get all the top-level operators as a quotation rather than execution them).
Note that this would add API noise to the DbContext surface which likely only few people will actually need. Maybe there's some way to combine ToQueryString() with the aforementioned Query() API, to help out with this.
Raised by @clement911 in #33143 (comment) and #33167.
The text was updated successfully, but these errors were encountered: