Esempio n. 1
0
        protected virtual void GenerateAnnotation(
            [NotNull] IAnnotation annotation, [NotNull] IndentedStringBuilder stringBuilder)
        {
            Check.NotNull(annotation, nameof(annotation));
            Check.NotNull(stringBuilder, nameof(stringBuilder));

            stringBuilder
            .Append(".HasAnnotation(")
            .Append(_code.Literal(annotation.Name))
            .Append(", ")
            .Append(_code.UnknownLiteral(annotation.Value))
            .Append(")");
        }
Esempio n. 2
0
        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.GetAnnotations(), builder);
            }
        }
Esempio n. 3
0
        protected virtual void GenerateEntityTypeAnnotations([NotNull] IEntityType entityType, [NotNull] IndentedStringBuilder stringBuilder)
        {
            Check.NotNull(entityType, nameof(entityType));
            Check.NotNull(stringBuilder, nameof(stringBuilder));

            var annotations         = entityType.GetAnnotations().ToList();
            var tableNameAnnotation = annotations.FirstOrDefault(a => a.Name == RelationalFullAnnotationNames.Instance.TableName);
            var schemaAnnotation    = annotations.FirstOrDefault(a => a.Name == RelationalFullAnnotationNames.Instance.Schema);

            stringBuilder
            .AppendLine()
            .AppendLine()
            .Append("b.")
            .Append(nameof(RelationalEntityTypeBuilderExtensions.ToTable))
            .Append("(")
            .Append(_code.Literal((string)tableNameAnnotation?.Value ?? entityType.DisplayName()));
            annotations.Remove(tableNameAnnotation);

            if (schemaAnnotation?.Value != null)
            {
                stringBuilder
                .Append(",")
                .Append(_code.Literal((string)schemaAnnotation.Value));
                annotations.Remove(schemaAnnotation);
            }

            stringBuilder.Append(");");

            var discriminatorPropertyAnnotation = annotations.FirstOrDefault(a => a.Name == RelationalFullAnnotationNames.Instance.DiscriminatorProperty);
            var discriminatorValueAnnotation    = annotations.FirstOrDefault(a => a.Name == RelationalFullAnnotationNames.Instance.DiscriminatorValue);

            if ((discriminatorPropertyAnnotation ?? discriminatorValueAnnotation) != null)
            {
                stringBuilder
                .AppendLine()
                .AppendLine()
                .Append("b.")
                .Append(nameof(RelationalEntityTypeBuilderExtensions.HasDiscriminator));

                if (discriminatorPropertyAnnotation?.Value != null)
                {
                    var propertyClrType = entityType.FindProperty((string)discriminatorPropertyAnnotation.Value)?.ClrType;
                    stringBuilder
                    .Append("<")
                    .Append(_code.Reference(propertyClrType.UnwrapEnumType()))
                    .Append(">(")
                    .Append(_code.UnknownLiteral(discriminatorPropertyAnnotation.Value))
                    .Append(")");
                }
                else
                {
                    stringBuilder
                    .Append("()");
                }

                if (discriminatorValueAnnotation?.Value != null)
                {
                    stringBuilder
                    .Append(".")
                    .Append(nameof(DiscriminatorBuilder.HasValue))
                    .Append("(")
                    .Append(_code.UnknownLiteral(discriminatorValueAnnotation.Value))
                    .Append(")");
                }

                stringBuilder.Append(";");

                annotations.Remove(discriminatorPropertyAnnotation);
                annotations.Remove(discriminatorValueAnnotation);
            }

            IgnoreAnnotations(
                annotations,
                RelationshipDiscoveryConvention.NavigationCandidatesAnnotationName,
                RelationshipDiscoveryConvention.AmbiguousNavigationsAnnotationName,
                InversePropertyAttributeConvention.InverseNavigationsAnnotationName);

            if (annotations.Any())
            {
                foreach (var annotation in annotations)
                {
                    stringBuilder
                    .AppendLine()
                    .AppendLine()
                    .Append("b");

                    GenerateAnnotation(annotation, stringBuilder);

                    stringBuilder.Append(";");
                }
            }
        }