コード例 #1
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<")
                        .Append(_code.Reference(column.ClrType))
                        .Append(">(");

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

                        if (column.ColumnType != null)
                        {
                            builder
                            .Append("type: ")
                            .Append(_code.Literal(column.ColumnType))
                            .Append(", ");
                        }

                        builder.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.UniqueConstraint(")
                        .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(foreignKey.Columns.Length == 1
                                    ? "column: "
                                    : "columns: ")
                            .Append(_code.Lambda(foreignKey.Columns.Select(c => map[c]).ToList()));

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

                            builder
                            .AppendLine(",")
                            .Append("principalTable: ")
                            .Append(_code.Literal(foreignKey.PrincipalTable))
                            .AppendLine(",")
                            .Append(
                                foreignKey.PrincipalColumns.Length == 1
                                        ? "principalColumn: "
                                        : "principalColumns: ")
                            .Append(_code.Literal(foreignKey.PrincipalColumns));

                            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);
            }
        }
コード例 #2
0
        public override string GenerateMigration(
            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());
        }