public void FirstCreateOfEntityInSessionFactoryAlsoIncrementsGeneratorId()
        {
            ResetDatabase();

            var person = new PersonWithBlockSizeDefault {FirstName = "Tim", LastName = "Howard"};

            var sessionFactory = new ConfigurationFactory().CreateConfiguration().BuildSessionFactory();
            using (var session = sessionFactory.OpenSession())
            using (var tx = session.BeginTransaction())
            {
                session.Save(person);
                tx.Commit();
            }

            Assert.Equal(1, person.Id);
            Assert.Equal(1 + Int16.MaxValue, GetCurrentGeneratorValue(sessionFactory));
        }
 /// <summary>
 /// Deletes all data and resets the ID generators
 /// </summary>
 private static void ResetDatabase()
 {
     var sessionFactory = new ConfigurationFactory().CreateConfiguration().BuildSessionFactory();
     using (var session = sessionFactory.OpenSession())
     using (var tx = session.BeginTransaction())
     {
         var directSql = session.CreateSQLQuery(DatabaseHelper.ResetDatabaseScript());
         directSql.ExecuteUpdate();
         tx.Commit();
     }
 }
        public void GeneratorIsInitializedToZeroWithBlockSizeTwo()
        {
            ResetDatabase();

            // Note: zero should be skipped as an ID to conform to expected NHibernate behavior

            var sessionFactory = new ConfigurationFactory().CreateConfiguration().BuildSessionFactory();
            const long smallBlockSize = 2;

            // Set generator to zero
            using (var session = sessionFactory.OpenSession())
            using (var tx = session.BeginTransaction())
            {
                var sqlQuery = session.CreateSQLQuery("UPDATE hibernate_unique_key SET next_id = 0");
                sqlQuery.ExecuteUpdate();
                tx.Commit();
            }
            Assert.Equal(0, GetCurrentGeneratorValue(sessionFactory));

            // ** First save of entity should get ID 1 (generator incremented)
            var personWithSmallBlockSize = new PersonWithBlockSizeTwo { FirstName = "Tim", LastName = "Howard" };
            SavePersonInNewSession(sessionFactory, personWithSmallBlockSize);
            Assert.Equal(1, personWithSmallBlockSize.Id);
            Assert.Equal(smallBlockSize, GetCurrentGeneratorValue(sessionFactory));

            // ** Second save of entity should get ID 2 (generator incremented)
            personWithSmallBlockSize = new PersonWithBlockSizeTwo { FirstName = "Tim", LastName = "Howard" };
            SavePersonInNewSession(sessionFactory, personWithSmallBlockSize);
            Assert.Equal(2, personWithSmallBlockSize.Id);
            Assert.Equal(smallBlockSize * 2, GetCurrentGeneratorValue(sessionFactory));

            // ** Third save of entity should get ID 3 (generator not incremented)
            personWithSmallBlockSize = new PersonWithBlockSizeTwo { FirstName = "Tim", LastName = "Howard" };
            SavePersonInNewSession(sessionFactory, personWithSmallBlockSize);
            Assert.Equal(3, personWithSmallBlockSize.Id);
            Assert.Equal(smallBlockSize * 2, GetCurrentGeneratorValue(sessionFactory));

            // ** Fourth save of entity should get ID 4 (generator incremented)
            personWithSmallBlockSize = new PersonWithBlockSizeTwo { FirstName = "Tim", LastName = "Howard" };
            SavePersonInNewSession(sessionFactory, personWithSmallBlockSize);
            Assert.Equal(4, personWithSmallBlockSize.Id);
            Assert.Equal(smallBlockSize * 3, GetCurrentGeneratorValue(sessionFactory));
        }