Transaction.Commit 保存 BeginTransaction 之前所做的更改
避免进行不必要的更改的最佳方法是什么?
var a = session.Load<A>(id);
a.Value = ParseExpressions(a.Value);
using(var tx = session.BeginTransaction())
{
// Do some work here
tx.Commit();
}
A
对象包含在完成工作之前需要评估的表达式,但在提交事务时,a.Value
将保存到数据库中。
我只想保存 session.BeginTransaction() after 之后发生的更改。
我尝试调用 session.Clear()
但这会导致具有 Guid Id 的实体被保存而不是更新。
非常感谢任何帮助。
What is the best way to avoid committing unwanted changes?
var a = session.Load<A>(id);
a.Value = ParseExpressions(a.Value);
using(var tx = session.BeginTransaction())
{
// Do some work here
tx.Commit();
}
The A
object contains expressions which need to be evaluated before the work can be done, but when the transaction is committed a.Value
is saved to the database.
I only want the changes which occur after session.BeginTransaction() to be saved.
I've tried calling session.Clear()
but that causes entities with a Guid Id to be saved instead of updated.
Any help greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不能 100% 确定我没记错,但您应该能够将 session.FlushMode 设置为 never,并显式保存您想要保存的更改。
Not 100% sure I remember correctly, but you should be able to set the session.FlushMode to never, and explicitly save the changes you want to save.
您可以使用事件系统来跳过保存特定实体,但您会给自己带来麻烦。
您还可以从会话中
Evict()
特定实体。经验法则是:如果您不想保存对实体的更改,不要修改该实体。当您调用 commit 时,所有更改都必须准备就绪。
You could use the event system to skip saving specific entities, but you're getting yourself in trouble.
You can also
Evict()
specific entities from the session.The rule of thumb is: if you don't want to save a change to an entity, don't modify the entity. All your changes must be ready when you call commit.