0

I have the following configuration to set NH and Envers up:

var properties = new Dictionary<string, string>();
properties[NHibernate.Cfg.Environment.Dialect] = "NHibernate.Dialect.MsSql2008Dialect";
properties[NHibernate.Cfg.Environment.ConnectionDriver] = "NHibernate.Driver.SqlClientDriver";
properties[NHibernate.Cfg.Environment.Hbm2ddlAuto] = "update";
properties[NHibernate.Cfg.Environment.FormatSql] = "true";
properties[NHibernate.Cfg.Environment.ShowSql] = "true";
properties[NHibernate.Cfg.Environment.ConnectionString] = "Data Source=localhost;Initial Catalog=OU_KASH;Integrated Security=True;Asynchronous Processing=true";
var cfg = new Configuration();
cfg.Configure()
    .SetProperties(properties)
    .AddAssembly(typeof(AliasTb).Assembly.FullName)
    ;

cfg.SetEnversProperty(ConfigurationKey.StoreDataAtDelete, true);
cfg.SetEnversProperty(ConfigurationKey.AuditStrategy, typeof(NHibernate.Envers.Strategy.ValidityAuditStrategy));
cfg.SetEnversProperty(ConfigurationKey.TrackEntitiesChangedInRevision, true);
cfg.SetEnversProperty(ConfigurationKey.GlobalWithModifiedFlag, true);

cfg.IntegrateWithEnvers(new AttributeConfiguration());

var factory = cfg.BuildSessionFactory();

If I leave off the

cfg.SetEnversProperty(ConfigurationKey.AuditStrategy, typeof(NHibernate.Envers.Strategy.ValidityAuditStrategy));

it works fine, but with that option I get the following exception when I commit my transaction (which updates a CompanyAddressTb object):

ERROR NHibernate.AssertionFailure - An AssertionFailure occurred - this may indicate a bug in NHibernate or in your custom types.
System.InvalidOperationException: Cannot find previous revision for entity NhDoodling.Entities.Domain.CompanyAddressTb_AUD and id 12962904
   at NHibernate.Envers.Strategy.ValidityAuditStrategy.updateLastRevision(ISession session, AuditConfiguration auditCfg, IList l, Object id, String auditedEntityName, Object revision, Boolean throwIfNotOneEntry)
   at NHibernate.Envers.Strategy.ValidityAuditStrategy.Perform(ISession session, String entityName, Object id, Object data, Object revision)
   at NHibernate.Envers.Synchronization.Work.AbstractAuditWorkUnit.Perform(ISession session, Object revisionData)
   at NHibernate.Envers.Synchronization.AuditProcess.executeInSession(ISession executeSession)
   at NHibernate.Envers.Synchronization.AuditProcess.DoBeforeTransactionCompletion()
   at NHibernate.Envers.Synchronization.AuditProcessManager.<>c__DisplayClass4.<Get>b__0()
   at NHibernate.Engine.ActionQueue.BeforeTransactionCompletionProcessQueue.BeforeTransactionCompletion()

Any help will be greatly appreciated.

Michael
  • 1,351
  • 1
  • 11
  • 25

1 Answers1

0

If you start to use Envers on a non empty db, you need to manually insert current data in audit tables.

Read more here.

Community
  • 1
  • 1
Roger
  • 1,944
  • 1
  • 11
  • 17