In fact in Core the behavior is not the same as the normal EF and there is no ready form, this is one of its disadvantages in relation to the full, at least until now.
I found some solutions at Soen that show how to do it. It’s not a simple code, but it’s for you to create as a library for your use. I will reproduce here the solution that seemed most interesting:
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Query;
public static string ToSql<TEntity>(this IQueryable<TEntity> query) where TEntity : class {
var enumerator = query.Provider.Execute<IEnumerable<TEntity>>(query.Expression).GetEnumerator();
var relationalCommandCache = enumerator.Private("_relationalCommandCache");
var selectExpression = relationalCommandCache.Private<SelectExpression>("_selectExpression");
var factory = relationalCommandCache.Private<IQuerySqlGeneratorFactory>("_querySqlGeneratorFactory");
var sqlGenerator = factory.Create();
var command = sqlGenerator.GetCommand(selectExpression);
return command.CommandText;
}
private static object Private(this object obj, string privateField) => obj?.GetType().GetField(privateField, BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(obj);
private static T Private<T>(this object obj, string privateField) => (T)obj?.GetType().GetField(privateField, BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(obj);
I put in the Github for future reference.
Then you can apply that method to query that will do what the ToString()
did in EF 6. I think even more correct the form of Core, only that this method should be present in the ready-to-use Entity Framework.
If you do not need in the code have tools to see this, if it is only analysis and not part of the application can be even more interesting.
If you are running in a real database, you can use SQL Profiler to view the query.
– Gabriel Coletta
@Gabrielcoletta does not want to try to add an answer?
– Marconi
I could, but I don’t know if I’d add anything more to what’s already in the documentation. If you would like more details, just browse the documentation for SQL Profiler: https://docs.microsoft.com/pt-br/sql/tools/sql-server-profiler/sql-server-profiler?view=sql-server-ver15
– Gabriel Coletta