Esempio n. 1
0
 private static void CreateNewEntity(ISession session)
 {
     using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TimeSpan(1, 1, 1)))
     {
         var entity = new Cat
         {
             Name = "Princess1",
             Sex = 'F',
             Weight = 7.4f,
             BirthDate = DateTime.Now,
             Color = "Black"
         };
         session.Save(entity);
         scope.Complete();
     }
 }
Esempio n. 2
0
 public void NewCat()
 {
     ISession session = NHibernateHelper.GetCurrentSession();
     ITransaction tx = session.BeginTransaction();
     var princess = new Cat
     {
         Name = "Princess",
         Sex = 'F',
         Weight = 7.4f,
         BirthDate = DateTime.Now,
         Color = "Black"
     };
     session.Save(princess);
     tx.Commit();
     NHibernateHelper.CloseSession();
 }
Esempio n. 3
0
        public void CreateCatWithChildrenTest()
        {
            ISession session = NHibernateHelper.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();
            var mam = new Cat
                          {
                              Name = "MamCat",
                              Sex = 'F',
                              Weight = 7.4f,
                              BirthDate = DateTime.Now,
                              Color = "Black"
                          };
            session.Save(mam);
            for (int i = 0; i < 5; i++)
            {
                var name = "Cat_" + i.ToString();
                var kitt = CreateKitt(mam, name);
                session.Save(kitt);
            }

            session.Save(mam);
            tx.Commit();

            var query = session.QueryOver<Cat>();
            query.Where(c => c.Name == "MamCat");
            var result = query.List().FirstOrDefault();
            Assert.IsTrue(result != null && result.Kittens.Count > 0);
            NHibernateHelper.CloseSession();
        }
Esempio n. 4
0
 private void NHCreateEntity()
 {
     ISession session = NHibernateHelper.GetCurrentSession();
     //session.Transaction.
     Console.WriteLine("ITransaction创建前");
     DealTransaction();
     //ITransaction tx = session.BeginTransaction();
     var princess = new Cat
                        {
                            Name = "Princess",
                            Sex = 'F',
                            Weight = 7.4f,
                            BirthDate = DateTime.Now,
                            Color = "Black"
                        };
     session.Save(princess);
     Console.WriteLine("ITransaction提交前");
     DealTransaction();
     //tx.Commit();
     Console.WriteLine("Session关闭前");
     DealTransaction();
     //NHibernateHelper.CloseSession();
     Console.WriteLine("TScop提交前");
     DealTransaction();
 }
Esempio n. 5
0
 public void CreateSessionBeforeTransTest()
 {
     ISession session = NHibernateHelper.GetCurrentSession();
     TransInvoke.InvokTransFunction(() =>
                                        {
                                            var princess = new Cat
                                                               {
                                                                   Name = "Princess",
                                                                   Sex = 'F',
                                                                   Weight = 7.4f,
                                                                   BirthDate = DateTime.Now,
                                                                   Color = "Black"
                                                               };
                                            session.Save(princess);
                                            return 0;
                                        }, false);
     session.Flush();
 }
Esempio n. 6
0
 private static void CreateNewEntityWithoutTrans(ISession session)
 {
     var entity = new Cat
                      {
                          Name = "Princess1",
                          Sex = 'F',
                          Weight = 7.4f,
                          BirthDate = DateTime.Now,
                          Color = "Black"
                      };
     session.Save(entity);
 }
Esempio n. 7
0
        public void SessionWithComplexTransactionsTest()
        {
            ISession session = NHibernateHelper.GetCurrentSession();
            var cats = new List<Cat>();

            TransInvoke.InvokTransFunction(() =>
                                               {

                                                   TransInvoke.InvokTransFunction(() =>
                                                                                      {
                                                                                          var entity = new Cat
                                                                                                           {
                                                                                                               Name =
                                                                                                                   "Princess1",
                                                                                                               Sex = 'F',
                                                                                                               Weight =
                                                                                                                   7.4f,
                                                                                                               BirthDate
                                                                                                                   =
                                                                                                                   DateTime
                                                                                                                   .Now,
                                                                                                               Color =
                                                                                                                   "Black"
                                                                                                           };
                                                                                          session.Save(entity);
                                                                                          //cats.Add(entity);
                                                                                          return 0;
                                                                                      });
                                                   TransInvoke.InvokTransFunction(() =>
                                                                                      {
                                                                                          var entity = new Cat
                                                                                                           {
                                                                                                               Name =
                                                                                                                   "Princess2",
                                                                                                               Sex = 'F',
                                                                                                               Weight =
                                                                                                                   7.4f,
                                                                                                               BirthDate
                                                                                                                   =
                                                                                                                   DateTime
                                                                                                                   .Now,
                                                                                                               Color =
                                                                                                                   "Black"
                                                                                                           };
                                                                                          //cats.Add(entity);
                                                                                          session.Save(entity);
                                                                                          return 0;
                                                                                      });
                                                   foreach (var cat in cats)
                                                   {
                                                       session.Save(cat);
                                                   }
                                                   session.Flush();
                                                   return 0;
                                               }
                                           , true, true, null, TransactionScopeOption.Required);
        }
Esempio n. 8
0
        public void InnerTransTest()
        {
            TransInvoke.InvokTransFunction(() =>
                                               {
                                                   ISession session = NHibernateHelper.GetCurrentSession();
                                                   //Transaction.Current.TransactionCompleted +=
                                                   //    (object sender, TransactionEventArgs e) => NHibernateHelper.CloseSession();
                                                   using (ITransaction tx = session.BeginTransaction())
                                                   {
                                                       //Do sth...
                                                       var entity = new Cat
                                                                        {
                                                                            Name = "Princess",
                                                                            Sex = 'F',
                                                                            Weight = 7.4f,
                                                                            BirthDate = DateTime.Now,
                                                                            Color = "Black"
                                                                        };
                                                       session.Save(entity);
                                                       session.Delete(entity);
                                                       //session.Evict();
                                                       tx.Commit();

                                                       //throw new Exception("");
                                                   }
                                                   NHibernateHelper.CloseSession();
                                                   return 0;
                                               });
        }
Esempio n. 9
0
 private static Cat CreateKitt(Cat mam, string name)
 {
     var kitt = new Cat
                    {
                        Name = name,
                        Sex = 'F',
                        Weight = 7.4f,
                        BirthDate = DateTime.Now,
                        Color = "Black"
                    };
     mam.Kittens.Add(kitt);
     return kitt;
 }