6footunder
6footunder

Reputation: 1358

NHibernate PreUpdate event listener not persisting changes

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?

Upvotes: 4

Views: 2620

Answers (1)

Jamie Ide
Jamie Ide

Reputation: 49251

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/[email protected]/msg13624.html

Upvotes: 4

Related Questions