简体   繁体   中英

See parameters of UPDATEs in Fluent NHibernate when outputting statements to Console

Introduction

Following on from How to configure Fluent NHibernate to output queries to Trace or Debug instead of Console? the answer provided there nicely outputs information to the console with the exception that it outputs ? instead of the actual values of parameters.

Additionally using ShowSql() does not output any UPDATE lines.

Question

Is there a way to view the UPDATEs, parameters and all in the debug console?

Details of implementations

Using Interceptor

From How to configure Fluent NHibernate to output queries to Trace or Debug instead of Console? , I have implemented the following:

private class Interceptor : EmptyInterceptor
{
    public override SqlString OnPrepareStatement(SqlString sql)
    {
        var s = base.OnPrepareStatement(sql);
        Debug.WriteLine(s.ToString());
        return s;
    }
}
//... 

var factory = Fluently.Configure()
                      // ...
                      .ExposeConfiguration(c => c.SetInterceptor(new Interceptor())
                      // ...

which results in outputs like

UPDATE [User] SET Email = ?, HashedPassword = ?, Name = ? WHERE Id = ?

Using ShowSql()

From this blog I have implemented the following

public class CustomDebugWriter : System.IO.TextWriter
{
    public override void WriteLine(string value)
    {
        Debug.WriteLine(value);
        base.WriteLine(value);
    }
    public override void Write(string value)
    {
        Debug.Write(value);
        base.Write(value);
    }
    public override System.Text.Encoding Encoding
    {
        get { return new UTF8Encoding(); }
    }
}
// ...

Console.SetOut(new CustomDebugWriter());
var dbConfig = MsSqlConfiguration.MsSql2012.ConnectionString(
    c => c.FromConnectionStringWithKey(connectionStringKey));
dbConfig.ShowSql();

which doesn't output UPDATE statements at all.

It has to do with ISession's psuedo Unit Of Work and batching.

With Fluent-NHibernate you need to set the AdoNetBatchSize property:

dbConfig.AdoNetBatchSize(0);
dbConfig.ShowSql();
dbConfig.FormatSql();

Then after you do your update, you need to call Flush() to flush the "batch".

entity.Title = "test title";
Session.Update(entity);
Session.Flush();

It really depends on your architecture, where you call this, or if you are using your own Unit Of Work implementation. I only worry about the SQL output in my integration tests project so it's easy, I just call Flush on TearDown. Its probably not something you want to just throw in your App, it's usually best to just let NHibernate handle batch lifecycle and do its thing.

This is a workaround rather than a true answer.

If you're creating a web-app, you can use the Glimpse and NHibernate.Glimpse Nuget packages to examine what database calls are being made.

This has the parameters shown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM