Generating trigger-based auditing
Another approach to auditing involves tracking each change to an entity in a separate audit table. In this recipe, we will show you how to use NHibernate to generate audit triggers for your entity tables.
Getting ready
Download uNHAddIns.dll from the unofficial NHibernate AddIns project at https://bitbucket.org/fabiomaulo/unhaddins. Save the file to your solution's Lib folder.
How to do it…
- Create a new console application project with all standard NHibernate references, the standard NHibernate and log4net configuration, and the
Eg.Coremodel from Chapter 1, The Configuration and Schema. - Add a reference to
uNHAddIns.dll. - Set the
dialecttouNHAddIns.Audit.TriggerGenerator.ExtendedMsSql2008Dialect, uNHAddIns. - Add the following code to the
Mainmethod ofProgram.cs:var cfg = new Configuration().Configure(); var namingStrategy = new NamingStrategy(); var auditColumnSource = new AuditColumnSource(); new TriggerAuditing(cfg, namingStrategy, auditColumnSource...