-
-
Save saiwolf/57ada454ee2f5d9ca7e6bbf9a1ed7d3c to your computer and use it in GitHub Desktop.
(2.1 version by Bramvanelderen10) Resolve the SQL being executed behind the scenes in Entity Framework Core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.EntityFrameworkCore.Internal; | |
using Microsoft.EntityFrameworkCore.Query; | |
using Microsoft.EntityFrameworkCore.Query.Internal; | |
using Microsoft.EntityFrameworkCore.Storage; | |
using System; | |
using System.Linq; | |
using System.Reflection; | |
namespace Some.Namespace.Here | |
{ | |
public static class IQueryableExtensions | |
{ | |
private static readonly TypeInfo QueryCompilerTypeInfo = typeof(QueryCompiler).GetTypeInfo(); | |
private static readonly FieldInfo QueryCompilerField = typeof(EntityQueryProvider).GetTypeInfo().DeclaredFields.First(x => x.Name == "_queryCompiler"); | |
private static readonly FieldInfo QueryModelGeneratorField = QueryCompilerTypeInfo.DeclaredFields.First(x => x.Name == "_queryModelGenerator"); | |
private static readonly FieldInfo DataBaseField = QueryCompilerTypeInfo.DeclaredFields.Single(x => x.Name == "_database"); | |
private static readonly PropertyInfo DatabaseDependenciesField = typeof(Database).GetTypeInfo().DeclaredProperties.Single(x => x.Name == "Dependencies"); | |
public static string ToSql<TEntity>(this IQueryable<TEntity> query) where TEntity : class | |
{ | |
if (!(query is EntityQueryable<TEntity>) && !(query is InternalDbSet<TEntity>)) | |
{ | |
throw new ArgumentException("Invalid query"); | |
} | |
var queryCompiler = (QueryCompiler)QueryCompilerField.GetValue(query.Provider); | |
var modelGenerator = (QueryModelGenerator)QueryModelGeneratorField.GetValue(queryCompiler); | |
var queryModel = modelGenerator.ParseQuery(query.Expression); | |
var database = (IDatabase)DataBaseField.GetValue(queryCompiler); | |
var databaseDependencies = (DatabaseDependencies)DatabaseDependenciesField.GetValue(database); | |
var queryCompilerContext = databaseDependencies.QueryCompilationContextFactory.Create(false); | |
var modelVisitor = (RelationalQueryModelVisitor)queryCompilerContext.CreateQueryModelVisitor(); | |
modelVisitor.CreateQueryExecutor<TEntity>(queryModel); | |
var sql = modelVisitor.Queries.First().ToString(); | |
return sql; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the reply of the 2.1 version
The original article can be found here