コード例 #1
0
        public void ConstraintValidation_DeleteRootWithOneToManyChild_WithRestrictConstraint_ShouldThrowException()
        {
            IDbConnection connection = SetupTables();

            ITransaction transaction = CreateTransaction(connection);
            int          id          = 45;
            ConstraintTestDeleteRestrictRootEntity entity = new ConstraintTestDeleteRestrictRootEntity();

            entity.IdCol = id;
            entity.Name  = "Org-Name";
            entity.Persist(transaction);
            transaction.Commit();

            transaction = CreateTransaction(connection);
            ConstraintTestOne2ManyEntity one2ManyEntity = new ConstraintTestOne2ManyEntity();

            one2ManyEntity.IdCol = id;
            one2ManyEntity.Name  = "Child-Org-Name";
            one2ManyEntity.Persist(transaction);
            transaction.Commit();

            transaction = CreateTransaction(connection);
            ConstraintTestDeleteRestrictRootEntity loadedEntity = new ConstraintTestDeleteRestrictRootEntity();

            LoadEntityWithId(transaction, loadedEntity, id);
            loadedEntity.Status = EntityStatus.Deleted;
            Assert.Throws <PersistException>(() => loadedEntity.Persist(transaction));
            transaction.Commit();
            connection.Close();
        }
コード例 #2
0
        public void ConstraintValidation_DeleteOneToManyChild_WithCascadeConstraint_ShouldDeleteChild()
        {
            try
            {
                IDbConnection connection = SetupTables();

                ITransaction transaction = CreateTransaction(connection);
                int          id          = 45;
                ConstraintTestDeleteCascadeRootEntity entity = new ConstraintTestDeleteCascadeRootEntity();
                entity.IdCol = id;
                entity.Name  = "Org-Name";
                entity.Persist(transaction);
                transaction.Commit();

                transaction = CreateTransaction(connection);
                ConstraintTestOne2ManyEntity one2ManyEntity = new ConstraintTestOne2ManyEntity();
                one2ManyEntity.IdCol   = id;
                one2ManyEntity.IndexNo = 1;
                one2ManyEntity.Name    = "Child-Org-Name";
                one2ManyEntity.Persist(transaction);
                transaction.Commit();

                transaction = CreateTransaction(connection);
                ConstraintTestDeleteCascadeRootEntity loadedEntity = new ConstraintTestDeleteCascadeRootEntity();
                LoadEntityWithId(transaction, loadedEntity, id);
                IEnumerator <ConstraintTestOne2ManyEntity> childEnumarator = loadedEntity.One2ManyEntities.GetEnumerator();
                childEnumarator.MoveNext();
                ConstraintTestOne2ManyEntity loadedOne2ManyEntity = childEnumarator.Current;
                loadedOne2ManyEntity.Status = EntityStatus.Deleted;
                loadedEntity.Persist(transaction);
                transaction.Commit();

                transaction = CreateTransaction(connection);
                bool hasOneToMany = ExistsOne2ManyChild(transaction, id);
                bool hasRoot      = ExistsRoot(transaction, id);
                transaction.Commit();
                connection.Close();

                Assert.IsFalse(hasOneToMany);
                Assert.IsTrue(hasRoot);
            }
            catch (System.Exception e)
            {
                LogManager.GetLogger(typeof(DbGateConstraintValidationTest)).Fatal(e.Message, e);
                Assert.Fail(e.Message);
            }
        }