An NHibernate audit trail that doesn't cause “collection was not processed by flush” errors

前端 未结 4 1245
情深已故
情深已故 2020-12-31 18:14

Ayende has an article about how to implement a simple audit trail for NHibernate (here) using event handlers.

Unfortunately, as can be seen in the comments, his impl

4条回答
  •  无人及你
    2020-12-31 18:53

    I was able to solve the same problem using following workaround: set the processed flag to true on all collections in the current persistence context within the listener

    public void OnPostUpdate(PostUpdateEvent postEvent)
    {
        if (IsAuditable(postEvent.Entity))
        {
           //skip application specific code
    
            foreach (var collection in postEvent.Session.PersistenceContext.CollectionEntries.Values)
            {
                var collectionEntry = collection as CollectionEntry;
                collectionEntry.IsProcessed = true;
            }
    
            //var session = postEvent.Session.GetSession(EntityMode.Poco);
            //session.Save(auditTrailEntry);
            //session.Flush();
        }
    }
    

    Hope this helps.

提交回复
热议问题