NHibernate动态更新无法更新拦截器中更改的数据

时间:2009-11-20 16:19:47

标签: nhibernate

如果我设置dynamic-update = true,我发现在我的Interceptor中更新的字段不会包含在转到数据库的update语句中。当我将其设置为false时,包括时间戳在内的所有列都会更新。我真的想使用动态更新。

public class Interceptor : EmptyInterceptor
{
  public override Boolean OnFlushDirty(object entity, object id, object[] state,
    object[] previousState, string[] propertyNames, IType[] types)
  {
    var auditEntity = entity as BaseAuditEntity;

    if (auditEntity != null)
    {
      var now = DateTime.Now;
      var index = Array.IndexOf(propertyNames, "LastModifiedTimestamp");
      state[index] = now;
      auditEntity.LastModifiedTimestamp = now;
    }
    return base.OnFlushDirty(entity, id, state, previousState, propertyNames, types);
  }

}

我认为这一行会将我最后修改的列标记为脏。

auditEntity.LastModifiedTimestamp = now;

我的拦截器中是否应该做些什么来将时间戳字段标记为脏?

1 个答案:

答案 0 :(得分:3)

API-Doc说:“如果用户以任何方式修改了currentState,则返回true。”

您是否尝试返回true而不是调用空基础实现?

public class Interceptor : EmptyInterceptor
{
  public override Boolean OnFlushDirty(object entity, object id, object[] state,
    object[] previousState, string[] propertyNames, IType[] types)
  {
    var auditEntity = entity as BaseAuditEntity;

    if (auditEntity != null)
    {
      var now = DateTime.Now;
      var index = Array.IndexOf(propertyNames, "LastModifiedTimestamp");
      state[index] = now;
      auditEntity.LastModifiedTimestamp = now;

      return true;
    }
    return base.OnFlushDirty(entity, id, state, previousState, propertyNames, types);
  }

}