0

当我在城堡 Active Record 中调用FindAllByPropertyOnUpdate时,这会导致堆栈溢出,因为我对OnUpdate实例进行了一些重复检查。考虑以下代码。为什么它调用OnUpdate?怎么能阻止呢?

protected override void OnUpdate()
{
    if (FindAllByProperty("Title", this.Title).Length > 1)
        throw new Exception("duplicate Message in update");

    base.OnUpdate();
}
4

1 回答 1

2

以下是可能发生的事情:

  1. 您的应用程序中的某些内容会刷新您的会话。
  2. 在刷新时,NHibernate / ActiveRecord 执行您的 OnUpdate()
  3. OnUpdate() 调用 FindAllByProperty()
  4. FindAllByProperty() 尝试在同一个会话中运行查询,但会话仍然是脏的,所以 NHibernate 刷新会话。
  5. 回到 2。

因此,堆栈溢出。

为避免这种情况,请尝试在新会话中运行 FindAllByProperty():

using (new SessionScope())
  if (FindAllByProperty("Title", this.Title).Length > 1)
     throw new Exception("duplicate Message in update");
于 2010-07-25T17:07:57.903 回答