简体   繁体   English

.NET Core 3 / EF.Core 3:`QueryModelGenerator`、`RelationalQueryModelVisitor` 等发生了什么

[英].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.我想知道发生了什么: QueryModelGeneratorRelationalQueryModelVisitorqueryCompilationContext.CreateQuerymodelVisitor()似乎都消失了。

I have the following code and I'm failing to try to convert it to .NET Core 3 from 2.2我有以下代码,但我无法尝试将其从 2.2 转换为 .NET Core 3

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;
}

Isn't supported anymore in EF 3. 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在此处查看更多信息: https : //stackoverflow.com/a/51583047/196698 https://gist.github.com/rionmonster/2c59f449e67edf8cd6164e9fe66c545a#gistcomment-3059688

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .NET Core / EF.Core 3+ 将控制台日志记录添加到 DbContext - .NET Core / EF.Core 3+ add Console logging to DbContext EF Core 的 HasColumnName 发生了什么变化? - What happened to HasColumnName for EF Core? 如何在LINQ或EF.Core中执行此操作? - How to do this in LINQ or EF.Core? 使用 Code First 和 EF.Core 引用表以进行有效搜索的正确方法是什么 - What's the correct way to reference tables using Code First with EF.Core for searching efficiently 如何在 EF.core 中使用 FromSqlRaw 指定列 - How can I specific columns using FromSqlRaw in EF.core 从 EF.core 2.2 迁移到 EF.core 3.1 会破坏生成的 sql,将 1 添加到某些列名 - migrating to EF.core 3.1 from EF.core 2.2 breaks generated sql, adds 1 to some column names EF.Core加载其他表中没有相关记录的所有记录 - EF.Core load all records that don't have a related record in other table EF.Core 3.x生成窗口函数而不是JOIN,导致MySQL语法错误 - EF.Core 3.x generates window function instead of JOIN, leading to MySQL syntax error 如何在 EF.Core 5 中添加具有相同外键的多个导航属性? - How can I add multiple navigation properties with the same Foreign Key in EF.Core 5? 如何使用 EF.Core Database First 包含特殊字符 $ 脚手架表 - How do I Scaffold tables with EF.Core Database First containing special characters $
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM