protected virtual void Generate([NotNull] AddColumnOperation operation, [NotNull] IndentedStringBuilder builder)
        {
            Check.NotNull(operation, nameof(operation));
            Check.NotNull(builder, nameof(builder));

            builder
            .Append(".AddColumn<")
            .Append(_code.Reference(operation.ClrType))
            .AppendLine(">(");

            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(",")
                .Append("table: ")
                .Append(_code.Literal(operation.Table));

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

                builder.AppendLine(",")
                .Append("nullable: ")
                .Append(_code.Literal(operation.IsNullable));

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

                builder.Append(")");

                Annotations(operation.Annotations, builder);
            }
        }
Example #2
0
        public override string GenerateMetadata(
            string migrationNamespace,
            Type contextType,
            string migrationName,
            string migrationId,
            IModel targetModel)
        {
            Check.NotEmpty(migrationNamespace, nameof(migrationNamespace));
            Check.NotNull(contextType, nameof(contextType));
            Check.NotEmpty(migrationName, nameof(migrationName));
            Check.NotEmpty(migrationId, nameof(migrationId));
            Check.NotNull(targetModel, nameof(targetModel));

            var builder    = new IndentedStringBuilder();
            var namespaces = new List <string>
            {
                "System",
                "Microsoft.Data.Entity",
                "Microsoft.Data.Entity.Infrastructure",
                "Microsoft.Data.Entity.Metadata",
                "Microsoft.Data.Entity.Migrations",
                contextType.Namespace
            };

            namespaces.AddRange(GetNamespaces(targetModel));
            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("[DbContext(typeof(").Append(_code.Reference(contextType)).AppendLine("))]")
                .Append("[Migration(").Append(_code.Literal(migrationId)).AppendLine(")]")
                .Append("partial class ").AppendLine(_code.Identifier(migrationName))
                .AppendLine("{");
                using (builder.Indent())
                {
                    builder
                    .AppendLine("protected override void BuildTargetModel(ModelBuilder modelBuilder)")
                    .AppendLine("{");
                    using (builder.Indent())
                    {
                        // TODO: Optimize. This is repeated below
                        _modelGenerator.Generate("modelBuilder", targetModel, builder);
                    }
                    builder.AppendLine("}");
                }
                builder.AppendLine("}");
            }
            builder.AppendLine("}");

            return(builder.ToString());
        }