public void TestDbLocationModifierForImport()

in SampleTests/TestDbLocationModifiers.cs [124:177]


        public void TestDbLocationModifierForImport()
        {
            // Given database name, and path to save to
            string dbName = TestContext.TestName;
            string dataFolder = GetTestDir();
            string filePrefix = "mydb";
            string bacpacPath = Path.Combine(dataFolder, dbName + ".bacpac");
            string mdfFilePath = Path.Combine(dataFolder, filePrefix + "_Primary.mdf");
            string ldfFilePath = Path.Combine(dataFolder, filePrefix + "_Primary.ldf");
            
            // Delete any existing artifacts from a previous run
            TestUtils.DropDbAndDeleteFiles(dbName, mdfFilePath, ldfFilePath);

            SqlTestDB importedDb = null;
            try
            {
                // Create a DB and export
                SqlTestDB db = _trash.Add(TestUtils.CreateTestDatabase(TestUtils.DefaultInstanceInfo, "MyOriginalDb"));
                db.Execute(CreateOneTable);
                db.ExportBacpac(bacpacPath);
                
                // When deploying using the location modifying contributor
                DacImportOptions options = new DacImportOptions();
                options.ImportContributors = DbLocationModifier.ContributorId;

                options.ImportContributorArguments =
                    Utils.BuildContributorArguments(new Dictionary<string, string>()
                    {
                    {DbLocationModifier.DbSaveLocationArg, dataFolder},
                    {DbLocationModifier.DbFilePrefixArg, filePrefix},
                    });
                
                importedDb = SqlTestDB.CreateFromBacpac(TestUtils.DefaultInstanceInfo, bacpacPath, options, true);
                
                // Then expect the database to be saved under that path
                AssertDeploySucceeded(importedDb.BuildConnectionString(), importedDb.DatabaseName);
                Assert.IsTrue(File.Exists(mdfFilePath));
                Assert.IsTrue(File.Exists(ldfFilePath));

                // Note: for a real application, after creating the DB on the server they may want to
                // detach it and reattach using the database path. We are not doing this since it's
                // not relevant to this test
            }
            finally
            {
                if(importedDb != null)
                {
                    importedDb.Dispose();
                }
                TestUtils.DeleteIfExists(bacpacPath);
                TestUtils.DeleteIfExists(mdfFilePath);
                TestUtils.DeleteIfExists(ldfFilePath);
            }
        }