public virtual ScaffoldedMigration ScaffoldMigration(
            [NotNull] string migrationName,
            [NotNull] string rootNamespace)
        {
            Check.NotEmpty(migrationName, nameof(migrationName));
            Check.NotEmpty(rootNamespace, nameof(rootNamespace));

            if (_migrationAssembly.Migrations.Any(
                    m => _idGenerator.GetName(m.Id).Equals(migrationName, StringComparison.OrdinalIgnoreCase)))
            {
                throw new InvalidOperationException(Strings.DuplicateMigrationName(migrationName));
            }

            var lastMigration      = _migrationAssembly.Migrations.LastOrDefault();
            var migrationNamespace = lastMigration?.GetType().Namespace ?? rootNamespace + ".Migrations";
            var modelSnapshot      = _migrationAssembly.ModelSnapshot;
            var lastModel          = modelSnapshot?.Model;
            var upOperations       = _modelDiffer.GetDifferences(lastModel, _model);
            var downOperations     = upOperations.Any()
                ? _modelDiffer.GetDifferences(_model, lastModel)
                : new List <MigrationOperation>();
            var migrationId            = _idGenerator.CreateId(migrationName);
            var modelSnapshotNamespace = modelSnapshot?.GetType().Namespace ?? migrationNamespace;
            var modelSnapshotName      = modelSnapshot?.GetType().Name ?? _contextType.Name + "ModelSnapshot";

            var migrationCode = _migrationCodeGenerator.Generate(
                migrationNamespace,
                migrationName,
                upOperations,
                downOperations);
            var migrationMetadataCode = _migrationCodeGenerator.GenerateMetadata(
                migrationNamespace,
                _contextType,
                migrationName,
                migrationId,
                ProductVersion,
                _model);
            var modelSnapshotCode = _migrationCodeGenerator.GenerateSnapshot(
                modelSnapshotNamespace,
                _contextType,
                modelSnapshotName,
                _model);

            return(new ScaffoldedMigration(
                       _migrationCodeGenerator.Language,
                       lastMigration?.Id,
                       migrationCode,
                       migrationId,
                       migrationMetadataCode,
                       GetSubnamespace(rootNamespace, migrationNamespace),
                       modelSnapshotCode,
                       modelSnapshotName,
                       GetSubnamespace(rootNamespace, modelSnapshotNamespace)));
        }
Beispiel #2
0
        public override void CreateTables()
        {
            var operations = _modelDiffer.GetDifferences(null, Model);
            var statements = _migrationSqlGenerator.Generate(operations, Model);

            _executor.ExecuteNonQuery(_connection, null, statements);
        }
        public override void CreateTables(IModel model)
        {
            Check.NotNull(model, nameof(model));

            var operations = _modelDiffer.GetDifferences(null, model);
            var statements = _migrationSqlGenerator.Generate(operations, model);

            _executor.ExecuteNonQuery(_connection, null, statements);
        }
Beispiel #4
0
 private IEnumerable <SqlBatch> CreateSchemaCommands(IModel model)
 => _sqlGenerator.Generate(_modelDiffer.GetDifferences(null, model), model);