ExecuteSqlCommand() public static method

public static ExecuteSqlCommand ( string serverName, string databaseName, string sql ) : void
serverName string
databaseName string
sql string
return void
示例#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);
            }
        }