public override string Generate(
            string migrationNamespace,
            string migrationName,
            IReadOnlyList <MigrationOperation> upOperations,
            IReadOnlyList <MigrationOperation> downOperations)
        {
            Check.NotEmpty(migrationNamespace, nameof(migrationNamespace));
            Check.NotEmpty(migrationName, nameof(migrationName));
            Check.NotNull(upOperations, nameof(upOperations));
            Check.NotNull(downOperations, nameof(downOperations));

            var builder = new IndentedStringBuilder();

            builder
            .AppendLine("using System.Collections.Generic;")
            .AppendLine("using Microsoft.Data.Entity.Relational.Migrations;")
            .AppendLine("using Microsoft.Data.Entity.Relational.Migrations.Builders;")
            .AppendLine("using Microsoft.Data.Entity.Relational.Migrations.Operations;")
            .AppendLine()
            .Append("namespace ").AppendLine(migrationNamespace)
            .AppendLine("{");
            using (builder.Indent())
            {
                builder
                .Append("public partial class ").Append(_code.Identifier(migrationName)).AppendLine(" : Migration")
                .AppendLine("{");
                using (builder.Indent())
                {
                    builder
                    .AppendLine("public override void Up(MigrationBuilder migration)")
                    .AppendLine("{");
                    using (builder.Indent())
                    {
                        _operationGenerator.Generate("migration", upOperations, builder);
                    }
                    builder
                    .AppendLine("}")
                    .AppendLine()
                    .AppendLine("public override void Down(MigrationBuilder migration)")
                    .AppendLine("{");
                    using (builder.Indent())
                    {
                        _operationGenerator.Generate("migration", downOperations, builder);
                    }
                    builder.AppendLine("}");
                }
                builder.AppendLine("}");
            }
            builder.AppendLine("}");

            return(builder.ToString());
        }
示例#2
0
        protected virtual void Generate([NotNull] CreateTableOperation operation, [NotNull] IndentedStringBuilder builder)
        {
            Check.NotNull(operation, nameof(operation));
            Check.NotNull(builder, nameof(builder));

            builder.AppendLine(".CreateTable(");

            using (builder.Indent())
            {
                builder
                .Append("name: ")
                .Append(_code.Literal(operation.Name));

                if (operation.Schema != null)
                {
                    builder
                    .AppendLine(",")
                    .Append("schema: ")
                    .Append(_code.Literal(operation.Schema));
                }

                builder
                .AppendLine(",")
                .AppendLine("columns: table => new")
                .AppendLine("{");

                var map = new Dictionary <string, string>();
                using (builder.Indent())
                {
                    var scope = new List <string>();
                    for (var i = 0; i < operation.Columns.Count; i++)
                    {
                        var column       = operation.Columns[i];
                        var propertyName = _code.Identifier(column.Name, scope);
                        map.Add(column.Name, propertyName);

                        builder
                        .Append(propertyName)
                        .Append(" = table.Column(");

                        if (propertyName != column.Name)
                        {
                            builder
                            .Append("name: ")
                            .Append(_code.Literal(column.Name))
                            .Append(", ");
                        }

                        builder
                        .Append("type: ")
                        .Append(_code.Literal(column.Type))
                        .Append(", nullable: ")
                        .Append(_code.Literal(column.IsNullable));

                        if (column.DefaultValueSql != null)
                        {
                            builder
                            .Append(", defaultValueSql: ")
                            .Append(_code.Literal(column.DefaultValueSql));
                        }
                        else if (column.ComputedColumnSql != null)
                        {
                            builder
                            .Append(", computedColumnSql: ")
                            .Append(_code.Literal(column.ComputedColumnSql));
                        }
                        else if (column.DefaultValue != null)
                        {
                            builder
                            .Append(", defaultValue: ")
                            .Append(_code.UnknownLiteral(column.DefaultValue));
                        }

                        builder.Append(")");

                        using (builder.Indent())
                        {
                            Annotations(column.Annotations, builder);
                        }

                        if (i != operation.Columns.Count - 1)
                        {
                            builder.Append(",");
                        }

                        builder.AppendLine();
                    }
                }

                builder
                .AppendLine("},")
                .AppendLine("constraints: table =>")
                .AppendLine("{");

                using (builder.Indent())
                {
                    if (operation.PrimaryKey != null)
                    {
                        builder
                        .Append("table.PrimaryKey(")
                        .Append(_code.Literal(operation.PrimaryKey.Name))
                        .Append(", ")
                        .Append(_code.Lambda(operation.PrimaryKey.Columns.Select(c => map[c]).ToList()))
                        .Append(")");

                        using (builder.Indent())
                        {
                            Annotations(operation.PrimaryKey.Annotations, builder);
                        }

                        builder.AppendLine(";");
                    }

                    foreach (var uniqueConstraint in operation.UniqueConstraints)
                    {
                        builder
                        .Append("table.Unique(")
                        .Append(_code.Literal(uniqueConstraint.Name))
                        .Append(", ")
                        .Append(_code.Lambda(uniqueConstraint.Columns.Select(c => map[c]).ToList()))
                        .Append(")");

                        using (builder.Indent())
                        {
                            Annotations(uniqueConstraint.Annotations, builder);
                        }

                        builder.AppendLine(";");
                    }

                    foreach (var foreignKey in operation.ForeignKeys)
                    {
                        builder.AppendLine("table.ForeignKey(");

                        using (builder.Indent())
                        {
                            builder
                            .Append("name: ")
                            .Append(_code.Literal(foreignKey.Name))
                            .AppendLine(",")
                            .Append("columns: ")
                            .Append(_code.Lambda(foreignKey.Columns.Select(c => map[c]).ToList()));

                            if (foreignKey.ReferencedSchema != null)
                            {
                                builder
                                .AppendLine(",")
                                .Append("referencedSchema: ")
                                .Append(_code.Literal(foreignKey.ReferencedSchema));
                            }

                            builder
                            .AppendLine(",")
                            .Append("referencedTable: ")
                            .Append(_code.Literal(foreignKey.ReferencedTable));

                            if (foreignKey.ReferencedColumns != null)
                            {
                                builder
                                .AppendLine(",")
                                .Append(
                                    foreignKey.ReferencedColumns.Length == 1
                                            ? "referencedColumn: "
                                            : "referencedColumns: ")
                                .Append(_code.Literal(foreignKey.ReferencedColumns));
                            }

                            if (foreignKey.OnUpdate != ReferentialAction.NoAction)
                            {
                                builder
                                .AppendLine(",")
                                .Append("onUpdate: ")
                                .Append(_code.Literal(foreignKey.OnUpdate));
                            }

                            if (foreignKey.OnDelete != ReferentialAction.NoAction)
                            {
                                builder
                                .AppendLine(",")
                                .Append("onDelete: ")
                                .Append(_code.Literal(foreignKey.OnDelete));
                            }

                            builder.Append(")");

                            Annotations(foreignKey.Annotations, builder);
                        }

                        builder.AppendLine(";");
                    }
                }

                builder.Append("})");

                Annotations(operation.Annotations, builder);
            }
        }
        public override string Generate(
            string migrationNamespace,
            string migrationName,
            IReadOnlyList <MigrationOperation> upOperations,
            IReadOnlyList <MigrationOperation> downOperations)
        {
            Check.NotEmpty(migrationNamespace, nameof(migrationNamespace));
            Check.NotEmpty(migrationName, nameof(migrationName));
            Check.NotNull(upOperations, nameof(upOperations));
            Check.NotNull(downOperations, nameof(downOperations));

            var builder    = new IndentedStringBuilder();
            var namespaces = new List <string>
            {
                "System",
                "System.Collections.Generic",
                "Microsoft.Data.Entity.Migrations"
            };

            namespaces.AddRange(GetNamespaces(upOperations.Concat(downOperations)));
            foreach (var n in namespaces.Distinct())
            {
                builder
                .Append("using ")
                .Append(n)
                .AppendLine(";");
            }
            builder
            .AppendLine()
            .Append("namespace ").AppendLine(_code.Namespace(migrationNamespace))
            .AppendLine("{");
            using (builder.Indent())
            {
                builder
                .Append("public partial class ").Append(_code.Identifier(migrationName)).AppendLine(" : Migration")
                .AppendLine("{");
                using (builder.Indent())
                {
                    builder
                    .AppendLine("protected override void Up(MigrationBuilder migrationBuilder)")
                    .AppendLine("{");
                    using (builder.Indent())
                    {
                        _operationGenerator.Generate("migrationBuilder", upOperations, builder);
                    }
                    builder
                    .AppendLine("}")
                    .AppendLine()
                    .AppendLine("protected override void Down(MigrationBuilder migrationBuilder)")
                    .AppendLine("{");
                    using (builder.Indent())
                    {
                        _operationGenerator.Generate("migrationBuilder", downOperations, builder);
                    }
                    builder.AppendLine("}");
                }
                builder.AppendLine("}");
            }
            builder.AppendLine("}");

            return(builder.ToString());
        }