Ejemplo n.º 1
0
        public void Using_TransactionManager_Insert_2records_increases_count_by2_then_removing_them_decreases_count_by2()
        {
            ApplicationContext.LocalContext.Clear();
            var list = TransactionContextUserList.GetList();
            int beforeInsertCount = list.Count;

            list.AddRange(new[]
            {
                new TransactionContextUser
                {
                    FirstName   = "First",
                    LastName    = "Last",
                    SmallColumn = "aaaa"
                },
                new TransactionContextUser
                {
                    FirstName   = "First1",
                    LastName    = "Last",
                    SmallColumn = "bbb"
                }
            });

            list.Save();

            int tCount = 0;

            foreach (var r in ApplicationContext.LocalContext.Keys)
            {
                if (r.ToString().StartsWith("__transaction:"))
                {
                    tCount++;
                }
            }
            Assert.AreEqual(0, tCount, "Transaction context should have been null");

            list = TransactionContextUserList.GetList();
            Assert.AreEqual(beforeInsertCount + 2, list.Count, "Data should have been saved.");

            list.Remove(list.Last(o => o.LastName == "Last"));
            list.Remove(list.Last(o => o.LastName == "Last"));

            list.Save();

            tCount = 0;
            foreach (var r in ApplicationContext.LocalContext.Keys)
            {
                if (r.ToString().StartsWith("__transaction:"))
                {
                    tCount++;
                }
            }
            Assert.AreEqual(0, tCount, "Transaction context should have been null");

            list = TransactionContextUserList.GetList();
            Assert.AreEqual(beforeInsertCount, list.Count, "Data should not have been saved.");
        }
Ejemplo n.º 2
0
 protected void Child_Insert(TransactionContextUserList parent)
 {
     using (TransactionManager <SqlConnection, SqlTransaction> manager = TransactionManager <SqlConnection, SqlTransaction> .GetManager(nameof(WellKnownValues.DataPortalTestDatabase), true))
     {
         using (SqlCommand command = new SqlCommand("INSERT INTO Table2(FirstName, LastName, SmallColumn) VALUES('" + ReadProperty(firstNameProperty) + "', '" + ReadProperty(lastNameProperty) + "', '" + ReadProperty(smallColumnProperty) + "')", manager.Transaction.Connection, manager.Transaction))
         {
             command.ExecuteNonQuery();
         }
     }
 }
Ejemplo n.º 3
0
 protected void Child_DeleteSelf(TransactionContextUserList parent)
 {
     using (TransactionManager <SqlConnection, SqlTransaction> manager = TransactionManager <SqlConnection, SqlTransaction> .GetManager(nameof(WellKnownValues.DataPortalTestDatabase), true))
     {
         using (SqlCommand command = new SqlCommand("Delete From Table2 Where FirstName = '" + ReadProperty(firstNameProperty) + "' And LastName = '" + ReadProperty(lastNameProperty) + "' And SmallColumn = '" + ReadProperty(smallColumnProperty) + "'", manager.Transaction.Connection, manager.Transaction))
         {
             command.ExecuteNonQuery();
         }
     }
 }
Ejemplo n.º 4
0
        public void Using_TransactionManager_Insert_of_2records_rolls_back_if_second_record_fails_insert()
        {
            ApplicationContext.LocalContext.Clear();
            var list    = TransactionContextUserList.GetList();
            int counter = list.Count;

            list.Add(new TransactionContextUser
            {
                FirstName   = "First",
                LastName    = "Last",
                SmallColumn = "aaaa"
            });

            list.Add(new TransactionContextUser
            {
                FirstName   = "First1",
                LastName    = "Last1",
                SmallColumn = "bbbbbbbbbbbbbb"
            });

            bool gotError = false;

            try
            {
                list.Save();
            }
            catch (DataPortalException ex)
            {
                // will be thrown from SQL server
                gotError = true;
            }

            Assert.IsTrue(gotError, "SQL should have thrown an error");
            int tCount = 0;

            foreach (var r in ApplicationContext.LocalContext.Keys)
            {
                if (r.ToString().StartsWith("__transaction:"))
                {
                    tCount++;
                }
            }
            Assert.AreEqual(0, tCount, "Transaction context should have been null");

            list = TransactionContextUserList.GetList();
            Assert.AreEqual(counter, list.Count, "Data should not have been saved.");
        }