Galen.Ci.EntityFramework.Tests.TestUtils.GetDeploymentHistoryRowCount C# (CSharp) Method

GetDeploymentHistoryRowCount() public static method

public static GetDeploymentHistoryRowCount ( string serverName, string databaseName, string contextKey ) : int
serverName string
databaseName string
contextKey string
return int
        public static int GetDeploymentHistoryRowCount(string serverName, string databaseName, string contextKey)
        {
            var connectionString =
                $"Server={serverName};Initial Catalog={databaseName};Integrated Security=true;Application Name=Galen.Ci.EntityFramework.Tests;";

            using (var conn = new SqlConnection(connectionString))
            {
                var createdDb = conn.CreateCommand();
                createdDb.CommandText =
                    $"SELECT COUNT(*) FROM [dbo].[__DeploymentHistory] WHERE [ContextKey] = '{contextKey}'";

                conn.Open();
                using (var reader = createdDb.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        return reader.GetInt32(0);
                    }
                }
            }

            return 0;
        }
	}

Usage Example

示例#1
0
        public void CorrectlyInitializesADatabaseAndDoesNotSeed()
        {
            string serverName               = @"(localdb)\mssqllocaldb";
            string databaseName             = string.Format("TestContext_{0}", Guid.NewGuid().ToString().Replace("-", string.Empty));
            var    expectedMigrationHistory = new[]
            {
                "201506161504528_InitialCreate"
            };

            var config = new DbDeploymentManagerConfiguration()
            {
                Mode = DeploymentMode.InitializeOnly,
                TargetAssemblyPath = TestUtils.BuildTestContextTestAssemblyPath(1),
                Database           = new DatabaseEndpoint {
                    ServerName = serverName, DatabaseName = databaseName
                },
                InitializationConfig = new InitializerConfigurationInfo
                {
                    Type = "Galen.Ci.EntityFramework.Initialization.CreateSecureSeededDatabaseIfNotExists`2[[Galen.Ci.EntityFramework.Tests.TestContext.Data.TestDbContext, Galen.Ci.EntityFramework.Tests.TestContext], [Galen.Ci.EntityFramework.Tests.TestContext.Data.TestDataSeeder, Galen.Ci.EntityFramework.Tests.TestContext]], Galen.Ci.EntityFramework.Initialization",
                    DisableForcedSeeding = true                     //Disable seeding
                }
            };

            try
            {
                var sut = new DbDeploymentManager(config, new AssemblyLoader(), new SqlClientDbConnectionInfoBuilder());
                sut.Deploy();

                var migrationHistory = TestUtils.GetMigrationHistory(serverName, databaseName, "Galen.Ci.EntityFramework.Tests.TestContext.Data.Migrations.Configuration");

                Assert.IsNotNull(migrationHistory);
                Assert.AreEqual(expectedMigrationHistory.Length, migrationHistory.Count());
                Assert.IsTrue(expectedMigrationHistory.SequenceEqual(migrationHistory));

                var startingRows = TestUtils.GetRows(serverName, databaseName, "dbo.BasicEntities");

                //Now that we are deployed, let's delete some data and see if the auto-seeding re-adds it
                TestUtils.ExecuteSqlCommand(serverName, databaseName, "DELETE FROM dbo.BasicEntities WHERE ID = 2");
                var postDeleteRows = TestUtils.GetRows(serverName, databaseName, "dbo.BasicEntities");

                Assert.IsTrue(postDeleteRows.Count() == (startingRows.Count() - 1));

                sut = new DbDeploymentManager(config, new AssemblyLoader(), new SqlClientDbConnectionInfoBuilder());
                sut.Deploy();

                var postSeedingRows = TestUtils.GetRows(serverName, databaseName, "dbo.BasicEntities");

                Assert.IsTrue(postSeedingRows.Count() == (startingRows.Count() - 1));

                Assert.AreEqual(1, TestUtils.GetDeploymentHistoryRowCount(serverName, databaseName, "Galen.Ci.EntityFramework.Tests.TestContext.Data.Migrations.Configuration"));
            }
            finally
            {
                //Be sure to clean up
                TestUtils.DropDatabase(config.Database.ServerName, config.Database.DatabaseName);
            }
        }
All Usage Examples Of Galen.Ci.EntityFramework.Tests.TestUtils::GetDeploymentHistoryRowCount