NHibernate PreUpdate event listener not persisting changes

寵の児 提交于 2019-12-29 08:52:10

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/4383420/nhibernate-preupdate-event-listener-not-persisting-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!