private void GenerateMaxLengthAttribute(IProperty property)
        {
            var maxLength = property.GetMaxLength();

            if (maxLength.HasValue)
            {
                var lengthAttribute = new AttributeWriter(
                    property.ClrType == typeof(string)
                        ? nameof(StringLengthAttribute)
                        : nameof(MaxLengthAttribute));

                lengthAttribute.AddParameter(_code.Literal(maxLength.Value));

                _sb.AppendLine(lengthAttribute.ToString());
            }
        }
        private void GenerateForeignKeyAttribute(INavigation navigation)
        {
            if (navigation.IsDependentToPrincipal())
            {
                if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey())
                {
                    var foreignKeyAttribute = new AttributeWriter(nameof(ForeignKeyAttribute));

                    foreignKeyAttribute.AddParameter(
                        _code.Literal(
                            string.Join(",", navigation.ForeignKey.Properties.Select(p => p.Name))));

                    _sb.AppendLine(foreignKeyAttribute.ToString());
                }
            }
        }
        private void GenerateInversePropertyAttribute(INavigation navigation)
        {
            if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey())
            {
                var inverseNavigation = navigation.FindInverse();

                if (inverseNavigation != null)
                {
                    var inversePropertyAttribute = new AttributeWriter(nameof(InversePropertyAttribute));

                    inversePropertyAttribute.AddParameter($"nameof({inverseNavigation.DeclaringEntityType.Name}.{inverseNavigation.Name})");

                    _sb.AppendLine(inversePropertyAttribute.ToString());
                }
            }
        }
        private void GenerateInversePropertyAttribute(INavigation navigation)
        {
            if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey())
            {
                var inverseNavigation = navigation.FindInverse();

                if (inverseNavigation != null)
                {
                    var inversePropertyAttribute = new AttributeWriter(nameof(InversePropertyAttribute));

                    inversePropertyAttribute.AddParameter(CSharpUtilities.DelimitString(inverseNavigation.Name));

                    _sb.AppendLine(inversePropertyAttribute.ToString());
                }
            }
        }
    private void GenerateInversePropertyAttribute(INavigation navigation)
    {
        if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey())
        {
            var inverseNavigation = navigation.Inverse;

            if (inverseNavigation != null)
            {
                var inversePropertyAttribute = new AttributeWriter(nameof(InversePropertyAttribute));

                inversePropertyAttribute.AddParameter(_code.Literal(inverseNavigation.Name));

                _sb.AppendLine(inversePropertyAttribute.ToString());
            }
        }
    }
Example #6
0
        private void GeneratePrecisionAttribute(IProperty property)
        {
            var precision = property.GetPrecision();

            if (precision.HasValue)
            {
                var precisionAttribute = new AttributeWriter(nameof(PrecisionAttribute));
                precisionAttribute.AddParameter(_code.Literal(precision.Value));

                var scale = property.GetScale();
                if (scale.HasValue)
                {
                    precisionAttribute.AddParameter(_code.Literal(scale.Value));
                }

                _sb.AppendLine(precisionAttribute.ToString());
            }
        }
        private void GenerateInversePropertyAttribute(INavigation navigation)
        {
            if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey())
            {
                var inverseNavigation = navigation.FindInverse();

                if (inverseNavigation != null)
                {
                    var inversePropertyAttribute = new AttributeWriter(nameof(InversePropertyAttribute));

                    inversePropertyAttribute.AddParameter(CSharpHelper.Literal(inverseNavigation.Name));

                    NavPropertyAnnotations.Add(new Dictionary <string, object>
                    {
                        { "nav-property-annotation", inversePropertyAttribute.ToString() },
                    });
                }
            }
        }
        private void GenerateForeignKeyAttribute(INavigation navigation)
        {
            if (navigation.IsDependentToPrincipal())
            {
                if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey())
                {
                    var foreignKeyAttribute = new AttributeWriter(nameof(ForeignKeyAttribute));

                    foreignKeyAttribute.AddParameter(
                        CSharpHelper.Literal(
                            string.Join(",", navigation.ForeignKey.Properties.Select(p => p.Name))));

                    NavPropertyAnnotations.Add(new Dictionary <string, object>
                    {
                        { "nav-property-annotation", foreignKeyAttribute.ToString() },
                    });
                }
            }
        }
        private void GenerateUnicodeAttribute(IProperty property)
        {
            if (property.ClrType != typeof(string))
            {
                return;
            }

            var unicode = property.IsUnicode();

            if (unicode.HasValue)
            {
                var unicodeAttribute = new AttributeWriter(nameof(UnicodeAttribute));
                if (!unicode.Value)
                {
                    unicodeAttribute.AddParameter(_code.Literal(false));
                }
                _sb.AppendLine(unicodeAttribute.ToString());
            }
        }
        private void GenerateMaxLengthAttribute(IProperty property)
        {
            var maxLength = property.GetMaxLength();

            if (maxLength.HasValue)
            {
                var lengthAttribute = new AttributeWriter(
                    property.ClrType == typeof(string)
                        ? nameof(StringLengthAttribute)
                        : nameof(MaxLengthAttribute));

                lengthAttribute.AddParameter(CSharpHelper.Literal(maxLength.Value));

                PropertyAnnotationsData.Add(new Dictionary <string, object>
                {
                    { "property-annotation", lengthAttribute.ToString() },
                });
            }
        }
        private void GenerateInversePropertyAttribute(INavigation navigation)
        {
            if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey())
            {
                var inverseNavigation = navigation.FindInverse();

                if (inverseNavigation != null)
                {
                    var inversePropertyAttribute = new AttributeWriter(nameof(InversePropertyAttribute));

                    inversePropertyAttribute.AddParameter(
                        navigation.Name != inverseNavigation.DeclaringEntityType.Name
                            ? $"nameof({inverseNavigation.DeclaringEntityType.Name}.{inverseNavigation.Name})"
                            : this._code.Literal(inverseNavigation.Name));

                    this.IndentedStringBuilder.AppendLine(inversePropertyAttribute.ToString());
                }
            }
        }
        private void GenerateInversePropertyAttribute(INavigation navigation)
        {
            if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey())
            {
                var inverseNavigation = navigation.Inverse;

                if (inverseNavigation != null)
                {
                    var inversePropertyAttribute = new AttributeWriter(nameof(InversePropertyAttribute));

                    inversePropertyAttribute.AddParameter(
                        !navigation.DeclaringEntityType.GetPropertiesAndNavigations().Any(
                            m => m.Name == inverseNavigation.DeclaringEntityType.Name)
                            ? $"nameof({inverseNavigation.DeclaringEntityType.Name}.{inverseNavigation.Name})"
                            : _code.Literal(inverseNavigation.Name));

                    _sb.AppendLine(inversePropertyAttribute.ToString());
                }
            }
        }
Example #13
0
    private void GenerateIndexAttributes(IEntityType entityType)
    {
        // Do not generate IndexAttributes for indexes which
        // would be generated anyway by convention.
        foreach (var index in entityType.GetIndexes().Where(
                     i => ConfigurationSource.Convention != ((IConventionIndex)i).GetConfigurationSource()))
        {
            // If there are annotations that cannot be represented using an IndexAttribute then use fluent API instead.
            var annotations = _annotationCodeGenerator
                              .FilterIgnoredAnnotations(index.GetAnnotations())
                              .ToDictionary(a => a.Name, a => a);
            _annotationCodeGenerator.RemoveAnnotationsHandledByConventions(index, annotations);

            if (annotations.Count == 0)
            {
                var indexAttribute = new AttributeWriter(nameof(IndexAttribute));
                foreach (var property in index.Properties)
                {
                    // Do NOT use nameof for property.Name
                    indexAttribute.AddParameter(_code.Literal(property.Name));
                }

                if (index.Name != null)
                {
                    indexAttribute.AddParameter($"{nameof(IndexAttribute.Name)} = {_code.Literal(index.Name)}");
                }

                if (index.IsUnique)
                {
                    indexAttribute.AddParameter($"{nameof(IndexAttribute.IsUnique)} = {_code.Literal(index.IsUnique)}");
                }

                if (index.IsDescending is not null)
                {
                    indexAttribute.AddParameter($"{nameof(IndexAttribute.IsDescending)} = {_code.UnknownLiteral(index.IsDescending)}");
                }

                _sb.AppendLine(indexAttribute.ToString());
            }
        }
    }
Example #14
0
        private void GenerateForeignKeyAttribute(INavigation navigation)
        {
            if (navigation.IsOnDependent && navigation.ForeignKey.PrincipalKey.IsPrimaryKey())
            {
                var foreignKeyAttribute = new AttributeWriter(nameof(ForeignKeyAttribute));

                if (navigation.ForeignKey.Properties.Count > 1)
                {
                    foreignKeyAttribute.AddParameter(
                        code.Literal(
                            string.Join(",", navigation.ForeignKey.Properties.Select(p => p.Name))));
                }
                else
                {
#pragma warning disable CA1826 // Do not use Enumerable methods on indexable collections
                    foreignKeyAttribute.AddParameter($"nameof({navigation.ForeignKey.Properties.First().Name})");
#pragma warning restore CA1826 // Do not use Enumerable methods on indexable collections
                }

                sb.AppendLine(foreignKeyAttribute.ToString());
            }
        }
        private void GenerateTableAttribute(IEntityType entityType)
        {
            var tableName     = entityType.GetTableName();
            var schema        = entityType.GetSchema();
            var defaultSchema = entityType.Model.GetDefaultSchema();

            var schemaParameterNeeded = schema != null && schema != defaultSchema;
            var tableAttributeNeeded  = schemaParameterNeeded || tableName != null && tableName != entityType.GetDbSetName();

            if (tableAttributeNeeded)
            {
                var tableAttribute = new AttributeWriter(nameof(TableAttribute));

                tableAttribute.AddParameter(CSharpHelper.Literal(tableName));

                if (schemaParameterNeeded)
                {
                    tableAttribute.AddParameter($"{nameof(TableAttribute.Schema)} = {CSharpHelper.Literal(schema)}");
                }

                TemplateData.Add("class-annotation", tableAttribute.ToString());
            }
        }
        private void GenerateForeignKeyAttribute(INavigation navigation)
        {
            if (navigation.IsDependentToPrincipal())
            {
                if (navigation.ForeignKey.PrincipalKey.IsPrimaryKey())
                {
                    var foreignKeyAttribute = new AttributeWriter(nameof(ForeignKeyAttribute));

                    if (navigation.ForeignKey.Properties.Count > 1)
                    {
                        foreignKeyAttribute.AddParameter(
                            this._code.Literal(
                                string.Join(",", navigation.ForeignKey.Properties.Select(p => p.Name))));
                    }
                    else
                    {
                        foreignKeyAttribute.AddParameter($"nameof({navigation.ForeignKey.Properties.First().Name})");
                    }

                    this.IndentedStringBuilder.AppendLine(foreignKeyAttribute.ToString());
                }
            }
        }
        private void GenerateTableAttribute(IEntityType entityType)
        {
            var tableName     = entityType.Relational().TableName;
            var schema        = entityType.Relational().Schema;
            var defaultSchema = entityType.Model.Relational().DefaultSchema;

            var schemaParameterNeeded = schema != null && schema != defaultSchema;
            var tableAttributeNeeded  = schemaParameterNeeded || tableName != null && tableName != entityType.Scaffolding().DbSetName;

            if (tableAttributeNeeded)
            {
                var tableAttribute = new AttributeWriter(nameof(TableAttribute));

                tableAttribute.AddParameter(CSharpUtilities.DelimitString(tableName));

                if (schemaParameterNeeded)
                {
                    tableAttribute.AddParameter($"{nameof(TableAttribute.Schema)} = {CSharpUtilities.DelimitString(schema)}");
                }

                _sb.AppendLine(tableAttribute.ToString());
            }
        }
Example #18
0
        private void GenerateTableAttribute(IEntityType entityType)
        {
            var tableName     = entityType.GetTableName();
            var schema        = entityType.GetSchema();
            var defaultSchema = entityType.Model.GetDefaultSchema();

            var schemaParameterNeeded = schema != null && schema != defaultSchema;
            var tableAttributeNeeded  = schemaParameterNeeded || tableName != null && tableName != entityType.GetDbSetName();

            if (tableAttributeNeeded)
            {
                var tableAttribute = new AttributeWriter(nameof(TableAttribute));

                tableAttribute.AddParameter(_code.Literal(tableName));

                if (schemaParameterNeeded)
                {
                    tableAttribute.AddParameter($"{nameof(TableAttribute.Schema)} = {_code.Literal(schema)}");
                }

                _sb.AppendLine(tableAttribute.ToString());
            }
        }
    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    protected virtual void GenerateEntityTypeDataAnnotations(IEntityType entityType)
    {
        GenerateKeylessAttribute(entityType);
        GenerateTableAttribute(entityType);
        GenerateIndexAttributes(entityType);

        var annotations = _annotationCodeGenerator
                          .FilterIgnoredAnnotations(entityType.GetAnnotations())
                          .ToDictionary(a => a.Name, a => a);

        _annotationCodeGenerator.RemoveAnnotationsHandledByConventions(entityType, annotations);

        foreach (var attribute in _annotationCodeGenerator.GenerateDataAnnotationAttributes(entityType, annotations))
        {
            var attributeWriter = new AttributeWriter(attribute.Type.Name);
            foreach (var argument in attribute.Arguments)
            {
                attributeWriter.AddParameter(_code.UnknownLiteral(argument));
            }

            _sb.AppendLine(attributeWriter.ToString());
        }
    }
        private void GenerateTableAttribute(IEntityType entityType)
        {
            var tableName     = entityType.Relational().TableName;
            var schema        = entityType.Relational().Schema;
            var defaultSchema = entityType.Model.Relational().DefaultSchema;

            var schemaParameterNeeded = schema != null && schema != defaultSchema;
            var tableAttributeNeeded  = schemaParameterNeeded || tableName != null && tableName != entityType.Scaffolding().DbSetName;

            if (tableAttributeNeeded)
            {
                var tableAttribute = new AttributeWriter(nameof(TableAttribute));

                tableAttribute.AddParameter(_code.Literal(tableName));

                if (schemaParameterNeeded)
                {
                    tableAttribute.AddParameter($"{nameof(TableAttribute.Schema)} = {_code.Literal(schema)}");
                }

                _templateData.Add("class-annotation", tableAttribute.ToString());
            }
        }
Example #21
0
        private void GenerateTableAttribute(IEntityType entityType)
        {
            var tableName     = entityType.Relational().TableName;
            var schema        = entityType.Relational().Schema;
            var defaultSchema = entityType.Model.Relational().DefaultSchema;

            var schemaParameterNeeded = schema != null && schema != defaultSchema;
            var tableAttributeNeeded  = schemaParameterNeeded || tableName != null && tableName != entityType.Scaffolding().DbSetName;

            if (tableAttributeNeeded)
            {
                var tableAttribute = new AttributeWriter(nameof(TableAttribute));

                tableAttribute.AddParameter(this.code.Literal(tableName));

                if (schemaParameterNeeded)
                {
                    tableAttribute.AddParameter($"{nameof(TableAttribute.Schema)} = {this.code.Literal(schema)}");
                }

                this.IndentedStringBuilder.AppendLine(tableAttribute.ToString());
            }
        }
        private void GenerateTableAttribute(IEntityType entityType)
        {
            var tableName     = entityType.GetTableName();
            var schema        = entityType.GetSchema();
            var defaultSchema = entityType.Model.GetDefaultSchema();

            var schemaParameterNeeded = schema != null && schema != defaultSchema;
            var isView = entityType.FindAnnotation(RelationalAnnotationNames.ViewDefinition) != null;
            var tableAttributeNeeded = !isView && (schemaParameterNeeded || tableName != null && tableName != entityType.GetDbSetName());

            if (tableAttributeNeeded)
            {
                var tableAttribute = new AttributeWriter(nameof(TableAttribute));

                tableAttribute.AddParameter(this._code.Literal(tableName));

                if (schemaParameterNeeded)
                {
                    tableAttribute.AddParameter($"{nameof(TableAttribute.Schema)} = {this._code.Literal(schema)}");
                }

                this.IndentedStringBuilder.AppendLine(tableAttribute.ToString());
            }
        }