예제 #1
0
        /// <summary>
        ///     Saves a scaffolded migration to files.
        /// </summary>
        /// <param name="projectDir"> The project's root directory. </param>
        /// <param name="migration"> The scaffolded migration. </param>
        /// <param name="outputDir"> The directory to put files in. Paths are relative to the project directory. </param>
        /// <returns> The saved migrations files. </returns>
        public virtual MigrationFiles Save(string projectDir, ScaffoldedMigration migration, string outputDir)
        {
            Check.NotEmpty(projectDir, nameof(projectDir));
            Check.NotNull(migration, nameof(migration));

            var lastMigrationFileName  = migration.PreviousMigrationId + migration.FileExtension;
            var migrationDirectory     = outputDir ?? GetDirectory(projectDir, lastMigrationFileName, migration.MigrationSubNamespace);
            var migrationFile          = Path.Combine(migrationDirectory, migration.MigrationId + migration.FileExtension);
            var migrationMetadataFile  = Path.Combine(migrationDirectory, migration.MigrationId + ".Designer" + migration.FileExtension);
            var modelSnapshotFileName  = migration.SnapshotName + migration.FileExtension;
            var modelSnapshotDirectory = outputDir ?? GetDirectory(projectDir, modelSnapshotFileName, migration.SnapshotSubnamespace);
            var modelSnapshotFile      = Path.Combine(modelSnapshotDirectory, modelSnapshotFileName);

            Dependencies.OperationReporter.WriteVerbose(DesignStrings.WritingMigration(migrationFile));
            Directory.CreateDirectory(migrationDirectory);
            File.WriteAllText(migrationFile, migration.MigrationCode, Encoding.UTF8);
            File.WriteAllText(migrationMetadataFile, migration.MetadataCode, Encoding.UTF8);

            Dependencies.OperationReporter.WriteVerbose(DesignStrings.WritingSnapshot(modelSnapshotFile));
            Directory.CreateDirectory(modelSnapshotDirectory);
            File.WriteAllText(modelSnapshotFile, migration.SnapshotCode, Encoding.UTF8);

            return(new MigrationFiles
            {
                MigrationFile = migrationFile, MetadataFile = migrationMetadataFile, SnapshotFile = modelSnapshotFile
            });
        }
        public virtual MigrationFiles Save([NotNull] string projectDir, [NotNull] ScaffoldedMigration migration)
        {
            Check.NotEmpty(projectDir, nameof(projectDir));
            Check.NotNull(migration, nameof(migration));

            var lastMigrationFileName  = migration.PreviousMigrationId + migration.FileExtension;
            var migrationDirectory     = GetDirectory(projectDir, lastMigrationFileName, migration.MigrationSubNamespace);
            var migrationFile          = Path.Combine(migrationDirectory, migration.MigrationId + migration.FileExtension);
            var migrationMetadataFile  = Path.Combine(migrationDirectory, migration.MigrationId + ".Designer" + migration.FileExtension);
            var modelSnapshotFileName  = migration.SnapshotName + migration.FileExtension;
            var modelSnapshotDirectory = GetDirectory(projectDir, modelSnapshotFileName, migration.SnapshotSubnamespace);
            var modelSnapshotFile      = Path.Combine(modelSnapshotDirectory, modelSnapshotFileName);

            _logger.Value.LogDebug(CommandsStrings.WritingMigration(migrationFile));
            Directory.CreateDirectory(migrationDirectory);
            File.WriteAllText(migrationFile, migration.MigrationCode);
            File.WriteAllText(migrationMetadataFile, migration.MetadataCode);

            _logger.Value.LogDebug(CommandsStrings.WritingSnapshot(modelSnapshotFile));
            Directory.CreateDirectory(modelSnapshotDirectory);
            File.WriteAllText(modelSnapshotFile, migration.SnapshotCode);

            return(new MigrationFiles
            {
                MigrationFile = migrationFile,
                MetadataFile = migrationMetadataFile,
                SnapshotFile = modelSnapshotFile
            });
        }