强制 Orchard 存储库插入数据
Force Orchard repository to insert the data
当 session 开启时。当调用一个动作时,我在数据库中插入了一些数据,稍后我可以使用其他动作来处理这些数据,一切都很好。
存储库正在获取数据,一切正常,但是一旦 session 终止,即使 session 在存储库上,数据也不会插入数据库,但已成功检索数据.
如何强制 orchard 中的存储库插入数据?
DB 和 NHibernate ISession
之间的显式 sync 操作可以通过显式调用强制执行:session.Flush()
。文档:
9.6. Flush
小引用:
...
From time to time the ISession will execute the SQL statements needed
to synchronize the ADO.NET connection's state with the state of
objects held in memory. This process, flush, occurs by default at the
following points
...
Except when you explicitly Flush()
, there are absolutely no guarantees about when the Session executes the ADO.NET calls, only the order in which they are executed. However, NHibernate does guarantee that the queries methods will never return stale data; nor will they return the wrong data.
在 orchard 中,存储库实现包装了该调用:
Repository.cs,第 117 行
public virtual void Flush() {
Session.Flush();
}
因此,我们也可以这样做
var repo = ... // get repository
repo.Flush();
这将同步应用程序和数据库,即所有 INSERT 语句都将被执行。
当 session 开启时。当调用一个动作时,我在数据库中插入了一些数据,稍后我可以使用其他动作来处理这些数据,一切都很好。
存储库正在获取数据,一切正常,但是一旦 session 终止,即使 session 在存储库上,数据也不会插入数据库,但已成功检索数据.
如何强制 orchard 中的存储库插入数据?
DB 和 NHibernate ISession
之间的显式 sync 操作可以通过显式调用强制执行:session.Flush()
。文档:
9.6. Flush
小引用:
...
From time to time the ISession will execute the SQL statements needed to synchronize the ADO.NET connection's state with the state of objects held in memory. This process, flush, occurs by default at the following points
...
Except when you explicitly
Flush()
, there are absolutely no guarantees about when the Session executes the ADO.NET calls, only the order in which they are executed. However, NHibernate does guarantee that the queries methods will never return stale data; nor will they return the wrong data.
在 orchard 中,存储库实现包装了该调用:
Repository.cs,第 117 行
public virtual void Flush() {
Session.Flush();
}
因此,我们也可以这样做
var repo = ... // get repository
repo.Flush();
这将同步应用程序和数据库,即所有 INSERT 语句都将被执行。