我有以下代码:
using (var session = SessionFactory.OpenSession())
{
var entity = session.Get<Entity>(id);
entity.Property1 = "new value";
using (var tx = session.BeginTransaction())
{
entity.Property2 = "new value";
tx.Commit();
}
}
现在,我很困惑,当 tx.Commit()
时,什么将提交给数据库?
是只提交 Property2
(在事务范围部分),还是同时提交 Property1
和 Property2
?
最佳答案
您对持久对象所做的任何更改都将在刷新 session 时发送到数据库,并且提交事务将刷新 session 。请注意,在某些情况下, session 可能会自动刷新,例如使用数据库生成的标识符或发出查询时。
令人困惑的是,在 NHibernate 中,您可以拥有仅包含提交的事务 block 。为了便于阅读,我将其重写为:
using (var session = SessionFactory.OpenSession())
{
using (var tx = session.BeginTransaction())
{
var entity = session.Get<Entity>(id);
entity.Property1 = "new value";
entity.Property2 = "new value";
tx.Commit();
}
}
关于c# - NHibernate事务提交,什么会提交到数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15288348/