Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core 3 / EF.Core 3: what happened to `QueryModelGenerator`, `RelationalQueryModelVisitor` and etc

I'm wondering what happened to: QueryModelGenerator, RelationalQueryModelVisitor and queryCompilationContext.CreateQuerymodelVisitor() all seems to have vanished.

I have the following code and I'm failing to try to convert it to .NET Core 3 from 2.2

public static string ToSql<TEntity>(this IQueryable<TEntity> query, DbContext dbCtx)
{
    var modelGenerator = dbCtx.GetService<IQueryModelGenerator>();
    var queryModel = modelGenerator.ParseQuery(query.Expression);
    var databaseDependencies = dbCtx.GetService<DatabaseDependencies>();
    var queryCompilationContext = databaseDependencies.QueryCompilationContextFactory.Create(false);
    var modelVisitor = (RelationalQueryModelVisitor) queryCompilationContext.CreateQueryModelVisitor();
    modelVisitor.CreateQueryExecutor<TEntity>(queryModel);
    var sql = modelVisitor.Queries.FirstOrDefault()?.ToString();
    return sql;
}
like image 231
Node.JS Avatar asked Dec 11 '19 19:12

Node.JS


People also ask

When would you use EF6 vs EF core?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.

How do I see SQL query generated by Entity Framework Core?

Once the breakpoint is hit hover over query to expand then click on > Debug View > The dropdown beside the magnifying glass> Text Visualizer. There you have it, the SQL that will be sent to the database. Text Visualizer option showing the query generated by EF Core 5.

Is EF core compatible with .NET framework?

You can use EF Core in APIs and applications that require the full . NET Framework, as well as those that target only the cross-platform .

What is IEntityTypeConfiguration?

IEntityTypeConfiguration<TEntity> Interface Allows configuration for an entity type to be factored into a separate class, rather than in-line in OnModelCreating(ModelBuilder).


1 Answers

Isn't supported anymore in EF 3.

Here how to do it now

    public static string ToSql<TEntity>(this IQueryable<TEntity> query)
    {
        var enumerator = query.Provider.Execute<IEnumerable<TEntity>>(query.Expression).GetEnumerator();
        var enumeratorType = enumerator.GetType();
        var selectFieldInfo = enumeratorType.GetField("_selectExpression", BindingFlags.NonPublic | BindingFlags.Instance) ?? throw new InvalidOperationException($"cannot find field _selectExpression on type {enumeratorType.Name}");
        var sqlGeneratorFieldInfo = enumeratorType.GetField("_querySqlGeneratorFactory", BindingFlags.NonPublic | BindingFlags.Instance) ?? throw new InvalidOperationException($"cannot find field _querySqlGeneratorFactory on type {enumeratorType.Name}");
        var selectExpression = selectFieldInfo.GetValue(enumerator) as SelectExpression ?? throw new InvalidOperationException($"could not get SelectExpression");
        var factory = sqlGeneratorFieldInfo.GetValue(enumerator) as IQuerySqlGeneratorFactory ?? throw new InvalidOperationException($"could not get IQuerySqlGeneratorFactory");
        var sqlGenerator = factory.Create();
        var command = sqlGenerator.GetCommand(selectExpression);
        var sql = command.CommandText;
        return sql;
    }

See more here : https://stackoverflow.com/a/51583047/196698 https://gist.github.com/rionmonster/2c59f449e67edf8cd6164e9fe66c545a#gistcomment-3059688

like image 165
Zied Avatar answered Nov 15 '22 11:11

Zied