Capture the query TraceSql in LINQ c #

1

I have a query in LINQ and I need to capture the generated sql, but I did not succeed. The conversion to ObjectQuery that I found does not work. What is missing?

var sqlConsulta = (from a in Sessao.Query<RequisitorioNaoLevantado>()
                  where (a.Codvara == codigoVara) &&
                      (string.IsNullOrEmpty(numeroProcesso) || 
                       a.AcaoOriginaria.Equals(numeroProcesso)) 
                 select a);

var sqlGerado = (ObjectQuery)sqlConsulta;
var commandSql = sqlGerado.ToTraceString();

For sqlConsulta.ToString (), we have:

  

NHibernate.Linq.NhQueryable'1 [Template.Entities.Part.RequisitorNoUrePerformed]

The line (ObjectQuery) sqlQuery generates the exception:

  

"Cannont cast 'sqlConsulta' (which has an actual type of 'NHibernate.Linq.NhQueryable'1 [Template.Entities.Partner.NotReserved)' to 'System.Data.Objects.ObjectQuery"

    
asked by anonymous 14.08.2017 / 22:01

1 answer

1

Has a reference to Fluent NHibernate in stackoverflow in English here

Type an interceptor:

using NHibernate;
using System.Diagnostics;

public class SqlStatementInterceptor : EmptyInterceptor
{
    public override NHibernate.SqlCommand.SqlString OnPrepareStatement(NHibernate.SqlCommand.SqlString sql)
    {
        Trace.WriteLine(sql.ToString());
        return sql;
    }
}

At your connection manager, connect your Interceptor like this:

protected virtual void Configure(FluentConfiguration config)
{
    config.ExposeConfiguration(x =>
                                   {
                                       x.SetInterceptor(new SqlStatementInterceptor());
                                   });
}
    
15.08.2017 / 15:47