gpt4 book ai didi

c# - 强制 Orchard 存储库插入数据

转载 作者:太空宇宙 更新时间:2023-11-03 12:16:02 25 4
gpt4 key购买 nike

当 session 开启时。当调用一个 Action 时,我在数据库中插入了一些数据,稍后我可以使用其他 Action 来处理这些数据,一切都很好。

存储库正在获取数据并且一切正常,但是一旦 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 语句都将被执行。

关于c# - 强制 Orchard 存储库插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49734920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com