4

We have the following PreUpdate event listener:

public bool OnPreUpdate(PreUpdateEvent @event)
        {
            BaseBO entity = @event.Entity as BaseBO;
            if (entity == null)
                return false;

            var operatorName = "OpName";
            var utcDateTime = DateTime.Now.ToUniversalTime();

            Set(@event.Persister, @event.State, "ModifiedBy", "Fred & Barney");
            Set(@event.Persister, @event.State, "ModifiedDate", utcDateTime);

            entity.ModifiedBy = "fred & barney";
            entity.ModifiedDate = utcDateTime;

            return false;
        }

private void Set(IEntityPersister persister, object[] state, string propertyName, object value)
        {
            var index = Array.IndexOf(persister.PropertyNames, propertyName);
            if (index == -1)
                return;
            state[index] = value;
        }

Breakpoints on return statement indicate that the old / new state values and the entity properties have been updated to the expected values.

However running Sql profiler shows that the ModifiedDate / ModifiedBy values are not updated.

If I update the persistence code and set the ModifiedDate manually, Profiler shows the ModifiedDate being updated.

The mapping file for the majority of our entities is:

<property name="ModifiedDate" insert="false" />

Any thoughts as to what could be preventing the values set by the event listener from being propogated to the database?

6footunder
  • 1,318
  • 18
  • 29

1 Answers1

4

Do you have dynamic-update in your table mappings? There's a "bug" in NHibernate that prevents PreUpdate and PreInsert from working with dynamic-update. See http://www.mail-archive.com/nhusers@googlegroups.com/msg13624.html

Jamie Ide
  • 48,427
  • 16
  • 81
  • 117
  • Also see these questions: http://stackoverflow.com/questions/5087888/ipreupdateeventlistener-and-dynamic-update-true and http://stackoverflow.com/questions/857234/audit-logging-nhibernate – Rob Kent Sep 20 '11 at 09:48