I’m new to nhibernate (but used hibernate for java before).
I built a session factory for our sql server database (sql server enterprise edition 8)
ISessionFactory factory2 = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(@"user id=xx; password=xxx;server=xxx;initial catalog=xxx")
.ShowSql()
)
.Mappings(m => m.FluentMappings
.AddFromAssemblyOf<Program>())
.ExposeConfiguration(cfg => new SchemaValidator(cfg).Validate())
.BuildSessionFactory();
So I use the ShowSql() method to log the queries to the console.
In my programm I load / create two objects and want to persist them and then do a update on a column:
using (var session = sf.OpenSession())
{
session.FlushMode = FlushMode.Always;
using (var ta = session.BeginTransaction())
{
Console.ReadKey();
PMA pm = session.CreateCriteria<PMA>()
.Add(Restrictions.Eq("Name", "HANSER")).List<PMA>().FirstOrDefault();
if (pm == null)
{
pm = new PMA();
pm.Prio = "1";
pm.Name = "HANSER";
pm.Datum = DateTime.Now;
session.Save(pm);
}
Clip clip = new Clip();
clip.PMA = pm;
clip.sys_created = DateTime.Now;
clip.sys_name = "system name";
clip.Title = "Test";
session.Save(clip);
Console.ReadKey();
clip.Title = "PETERSEN";
session.SaveOrUpdate(clip);
session.Transaction.Commit();
session.Flush();
session.Dispose();
Console.ReadKey();
}
}
The first insert for the pm object will be logged on the console, but the other insert and the update for the clip object don’t appear in the console. When I look in the database, I see there is everything right, everything will be inserted and updated. But I want to see the query. I try to set flush mode to always and make a session.Flush() to the session at the end and then a session.Dispose(), but nothing changes.
When I use postgres (only change the sessionfactory), I see all query logs.
How can I let nhibernate log all queries for sql server ?
When using ADO.NET batching (on by default in SQL Server, which supports it), DML queries are not logged to the console.