bool DoProcess() { var hash = (GetCurrentDatabaseCreationHash()).Replace("/", "-").Replace("\\", "-"); lock (SyncLock) { ReferenceDatabaseName = TempDatabaseName + ".Ref"; CurrentHashDirectory = ProjectTempRoot.GetOrCreateSubDirectory(hash); ReferenceMDFFile = CurrentHashDirectory.GetFile(ReferenceDatabaseName + ".mdf"); ReferenceLDFFile = CurrentHashDirectory.GetFile(ReferenceDatabaseName + "_log.ldf"); lock (ProcessSyncLock) { var createdNewReference = CreateReferenceDatabase(); var tempDatabaseDoesntExist = !MasterDatabaseAgent.DatabaseExists(TempDatabaseName); if (MustRenew || createdNewReference || tempDatabaseDoesntExist) { RefreshTempDataWorld(); } } return(true); } }
protected override void CloneReferenceDatabaseToTemp() { // Make sure if it exists in database already, it's deleted first. DataAccessor.Current.DeleteDatabase(TempDatabaseName); var directory = ProjectTempRoot.GetOrCreateSubDirectory("Current"); var newMDFPath = directory.GetFile(TempDatabaseName + ".mdf"); var newLDFPath = directory.GetFile(TempDatabaseName + "_log.ldf"); try { ReferenceMDFFile.CopyTo(newMDFPath); ReferenceLDFFile.CopyTo(newLDFPath); } catch (IOException ex) { if (ex.InnerException != null && ex.InnerException is UnauthorizedAccessException) { throw new Exception("Consider setting the IIS Application Pool identity to LocalSystem.", ex); } throw; } var script = "CREATE DATABASE [{0}] ON (FILENAME = '{1}'), (FILENAME = '{2}') FOR ATTACH" .FormatWith(TempDatabaseName, newMDFPath.FullName, newLDFPath.FullName); using (new DatabaseContext(DataAccessor.Current.GetMasterConnectionString())) { try { DataAccessor.Current.ExecuteNonQuery(script); } catch (SqlException ex) { throw new Exception("Could not attach the database from file " + newMDFPath.FullName + "." + Environment.NewLine + "Hint: Ensure SQL instance service has access to the folder. E.g. 'Local Service' may not have access to '{0}'" + newMDFPath.Directory.FullName, ex); } } }