0

下面的代码演示了一种误导性的情况,即数据被提交到数据库,即使从未在事务上调用提交。

谁能解释为什么?

[TestFixture]
public class TestFixture
{
        [Test]
        public void Test()
        {
            var config = DoConfiguration();

            using(var factory = config.BuildSessionFactory())
            {
                using (var session = factory.OpenSession())
                {
                    CallSessionContext.Bind(session);

                    using(new TransactionScope())
                    {
                        using (session.BeginTransaction())
                        {
                            var myEntity = session
                               .CreateQuery("from myEntity")
                               .List<MyEntity>()[0];

                            myEntity.Name = "test name";
                        }

                        var myEntity2 = session
                           .CreateQuery("from myEntity")
                           .List<MyEntity>()[0];

                        myEntity2.Name = "test name";

                        session.Flush();
                    }

                    CallSessionContext.Unbind(factory);
                }
            }
        }
} 
4

1 回答 1

2

显式调用 session.flush()是持久化您的更改。在这篇文章中详细讨论

于 2009-10-19T01:08:08.813 回答