Ayende上有一篇有关如何使用事件处理程序为NHibernate(here)实现简单审核跟踪的文章。
不幸的是,从注释中可以看出,他的实现导致引发以下异常:collection(XXX)没有由flush()处理。
问题似乎是对脏属性上的ToString的隐式调用,如果脏属性也是映射实体,则可能导致麻烦。
我尽了最大的努力来构建一个可行的实现,但是没有运气。
有人知道可行的解决方案吗?
最佳答案
我可以使用以下解决方法解决相同的问题:在监听器中的当前持久性上下文中,将所有集合上的已处理标志设置为true
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();
}
}
希望这可以帮助。
关于nhibernate - 不会导致 "collection was not processed by flush"错误的NHibernate审核跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3090733/