IPreUpdateEventListener and dynamic-update=“true”

前端 未结 2 1313
粉色の甜心
粉色の甜心 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<int> 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:

    <listener type="flush-entity"
              class="MyFlushEntityEventListener, MyAssembly"/>
    
    0 讨论(0)
  • 2020-12-09 12:57

    I had the same issue, however I found I could work around it by using OnFlushDirty.

    You can find my solution here.

    0 讨论(0)
提交回复
热议问题