【发布时间】:2014-07-23 10:46:24
【问题描述】:
我正在尝试对我的实体实施简单的审核。可审计实体实现了ITimestampable 接口,该接口定义了DateAdded 和DateModified 属性。
我创建并注册了一个事件监听器来填充这些值。这是完整的代码。
internal class TimeStampEventListener : IPreUpdateEventListener, IPreInsertEventListener
{
public bool OnPreUpdate(PreUpdateEvent e)
{
if (e.Entity is ITimestampable)
{
(e.Entity as ITimestampable).DateModified = DateTime.Now;
}
return false;
}
public bool OnPreInsert(PreInsertEvent e)
{
if (e.Entity is ITimestampable)
{
(e.Entity as ITimestampable).DateAdded = DateTime.Now;
}
return false;
}
public void Register(Configuration configuration)
{
configuration.SetListener(ListenerType.PreInsert, this);
configuration.SetListener(ListenerType.PreUpdate, this);
}
}
现在,当我进行会话刷新时,会调用侦听器,正确设置审核属性,但大多数情况下它们不会保存到数据库中。 “大多数时候”我的意思是很少有价值观真正得到持久化。我不确定,但它看起来像应用程序启动后的第一次插入/更新,这更奇怪。
当然,首先我对实体进行了更改,更改是持久化的,但审计属性不是。
当我在分析器中查看生成的 SQL 时,我发现查询中发送的是 NULL 而不是当前时间,所以我猜这不是数据库问题。顺便提一句。我正在使用 MySQL,DateAdded 和 DateModified 列是 DATE 类型。
在 NHibernate 站点上,这些属性仅映射为 <property>。也许我错过了一些针对此类情况的“特殊”映射......?
我完全坚持这一点。任何形式的帮助表示赞赏。
【问题讨论】:
标签: c# mysql nhibernate orm