C# NHibernate事务提交,将向数据库提交什么?

C# NHibernate事务提交,将向数据库提交什么?,c#,nhibernate,transactions,C#,Nhibernate,Transactions,我有以下代码: 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(); } }

我有以下代码:

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();
  }
}
使用(var session=SessionFactory.OpenSession())
{
var entity=session.Get(id);
entity.Property1=“新值”;
使用(var tx=session.BeginTransaction())
{
entity.Property2=“新值”;
tx.Commit();
}
}
现在,我很困惑,当
tx.Commit()
时,将向数据库提交什么?
是否只有
属性2
(在事务范围部分)将被提交,或者
属性1
属性2
都将被提交?

将提交实体中的所有属性。在您的配置中,您可以设置一个将sql输出到控制台的设置,您可以看到每次提交时sql发送的查询。

刷新会话时,您对持久对象所做的任何更改都将发送到数据库,提交事务将刷新会话。请注意,在某些情况下,会话可能会自动刷新,例如在使用数据库生成的标识符或发出查询时

令人困惑的是,在NHibernate中,可以有只包含提交的事务块。为了便于阅读,我将其改写为:

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();
  }
}
使用(var session=SessionFactory.OpenSession())
{
使用(var tx=session.BeginTransaction())
{
var entity=session.Get(id);
entity.Property1=“新值”;
entity.Property2=“新值”;
tx.Commit();
}
}

ooo,那么,是从打开的会话提交更改,而不是从开始的会话提交更改吗?感谢您耐心的回答,我仍然有一个问题。
ISession.Flush()
ITransaction.Commit()之间有什么区别?两者都可以将数据同步到数据库,但使用
ITransaction
必须在提交之前开始,而
ISession.BeginTransaction()
是一个令人困惑的操作,因为会话的每次更改都将提交,但事务范围的更改(从
BeginTransaction()开始)不会提交
提交()
)。文档中对此进行了详细解释:。我的建议是将会话的FlushMode设置为提交和使用事务。