IPreUpdateEventListener and dynamic-update=“true”

前端 未结 2 1316
粉色の甜心
粉色の甜心 2020-12-09 12:25

I have been trying to do a very simple auditing scenario following Ayende\'s blog which seem to be the resource everyone is refering to when it comes to IPreUpdateEventListe

2条回答
  •  伪装坚强ぢ
    2020-12-09 12:40

    I ran into this exact issue. This is how I fixed it:

    public class MyFlushEntityEventListener : DefaultFlushEntityEventListener
    {
        protected override void DirtyCheck(FlushEntityEvent e)
        {
            base.DirtyCheck(e);
            if (e.DirtyProperties != null &&
                e.DirtyProperties.Any() &&
                //ITrackUpdate is my inteface for audited entities
                e.Entity is ITrackUpdate)
                e.DirtyProperties = e.DirtyProperties
                 .Concat(GetAdditionalDirtyProperties(e)).ToArray();
        }
    
        static IEnumerable GetAdditionalDirtyProperties(FlushEntityEvent @event)
        {
            yield return Array.IndexOf(@event.EntityEntry.Persister.PropertyNames, 
                                       "UpdateTime");
            yield return Array.IndexOf(@event.EntityEntry.Persister.PropertyNames, 
                                       "UpdateUser");
            //You can add any additional properties here.
            //Some of my entities do not track the user, for example.
        }
    }
    

    Then, just replace the event listener in the NH config file:

    
    

提交回复
热议问题