NHibernate db not updating

本文关键字:updating not db NHibernate | 更新日期: 2023-09-27 18:25:19

namespace HRProject.Test
{
    using NUnit.Framework;
    using NHibernate;
    using HRProject.Model;
    using HRProject.Setup;
    using HRProject.Utils;
    using NHibernate.Linq;
    [TestFixture]
    public class RepositoryTest
    {
        [SetUp]
        public void SetUp()
        {
            ISession session = SessionHelper.GetSession();
            using (ITransaction tx = session.BeginTransaction())
            {
                foreach (Cashier c in session.Query<Cashier>().ToList<Cashier>())
                {
                    session.Delete(c);
                }
                tx.Commit();
            }
            using (ITransaction tx = session.BeginTransaction())
            {
                Cashier c1 = new Cashier(){Description = "D.Rathnayaka"};
                Cashier c2 = new Cashier(){Description = "K.Kumara"};
                Cashier c3 = new Cashier(){Description = "R.Gunapala"};
                session.Save(c1);
                session.Save(c2);
                session.Save(c3);
                tx.Commit();
            }
        }
        [Test]
        public void TestLoadUp() 
        { 
            ISession session = SessionHelper.GetSession();
            using (ITransaction tx = session.BeginTransaction())
            {
                Repository<Cashier> repo = new Repository<Cashier>();
                Assert.AreEqual(3, repo.Items.Count);
                Assert.AreEqual(1, (from Cashier c in repo.Items
                           where c.Description == "D.Rathnayaka"
                           select true).Count());
                tx.Commit();
            }
        }
        [Test]
        public void TestUpdate()
        {
            ISession session = SessionHelper.GetSession();
            using (ITransaction tx = session.BeginTransaction())
            {
                Repository<Cashier> repo = new Repository<Cashier>();
                Cashier cas = (from Cashier c in repo.Items
                            where c.Description == "D.Rathnayaka"
                            select c).Single();
                cas.Description = "K.R";
                session.Flush();
                tx.Commit();
            }

            using (ITransaction tx = session.BeginTransaction())
            {
                Repository<Cashier> repo = new Repository<Cashier>();
                //Fails here//
                Assert.IsNotNull((from Cashier c in repo.Items
                                  where c.Description == "K.R"
                                  select c).Single());
            }
        }
    }
}

有人可以告诉我为什么这个测试失败吗?数据库也不会更新。存储库类如下:

namespace HRProject.Utils
{
    using NHibernate;
    using HRProject.Setup;
    using NHibernate.Linq;
    class Repository<T> where T : class
    {
        public IList<T> Items {get; set;}
        public Repository()
        {
            Items = new List<T>();
            ISession session = SessionHelper.GetSession();
            using (ITransaction tx = session.BeginTransaction())
            {
                List<T> list = session.Query<T>().ToList();
                foreach (T obj in list)
                {
                    Items.Add((T)session.Merge(obj));
                }
                tx.Commit();
            }
        }
        public void AddRepositoryObject(T obj)
        {
        }
    }
}

NHibernate db not updating

我不知道您的 SessionHelper.GetSession 方法的实现细节,但我怀疑它在每次调用时都会打开新会话。如果为真,则测试 TestUpdate 有两个独立的会话对象。在测试方法中打开一个会话,存储库打开另一个会话。您更新由第二个会话对象持有的实体,但在第一个会话中提交事务。每个交互应只有一个会话对象(每个会话的会话(。在 CurrentSessionContext 对象中存储会话。