private static void AddUniqueConstraintIfNecessary(EdmProperty property, ColumnStatement columnStatement)
        {
            var value = property.GetCustomAnnotation <UniqueAttribute>();

            if (value != null)
            {
                columnStatement.ColumnConstraints.Add(new UniqueConstraint {
                    OnConflict = value.OnConflict
                });
            }
        }
        private static void AddDefaultValueConstraintIfNecessary(EdmProperty property, ColumnStatement columnStatement)
        {
            var value = property.GetCustomAnnotation <SqlDefaultValueAttribute>();

            if (value != null)
            {
                columnStatement.ColumnConstraints.Add(new DefaultValueConstraint {
                    DefaultValue = value.DefaultValue
                });
            }
        }
        private static void AddCollationConstraintIfNecessary(EdmProperty property, ColumnStatement columnStatement)
        {
            var value = property.GetCustomAnnotation <CollateAttribute>();

            if (value != null)
            {
                columnStatement.ColumnConstraints.Add(new CollateConstraint {
                    CollationFunction = value.Collation
                });
            }
        }
        private void AddPrimaryKeyConstraintAndAdjustTypeIfNecessary(EdmProperty property, ColumnStatement columnStatement)
        {
            // Only handle a single primary key this way.
            if (keyMembers.Count() != 1 || !property.Equals(keyMembers.Single()))
            {
                return;
            }

            ConvertIntegerType(columnStatement);
            var primaryKeyConstraint = new PrimaryKeyConstraint();

            primaryKeyConstraint.Autoincrement = property.GetCustomAnnotation <AutoincrementAttribute>() != null;
            columnStatement.ColumnConstraints.Add(primaryKeyConstraint);
        }
Exemplo n.º 5
0
        private static void AddCollationConstraintIfNecessary(EdmProperty property, ColumnStatement columnStatement, Collation defaultCollation)
        {
            var collateAttribute = property.GetCustomAnnotation <CollateAttribute>();

            if (property.PrimitiveType.PrimitiveTypeKind == PrimitiveTypeKind.String)
            {
                // The column is a string type. Check if we have an explicit or default collation.
                // If we have both, the explicitly chosen collation takes precedence.
                var value = collateAttribute == null ? defaultCollation : collateAttribute.Collation;
                if (value != null)
                {
                    columnStatement.ColumnConstraints.Add(new CollateConstraint {
                        CollationFunction = value.Function, CustomCollationFunction = value.CustomFunction
                    });
                }
            }
            else if (collateAttribute != null)
            {
                // Only string columns can be explicitly decorated with CollateAttribute.
                var name         = $"{property.DeclaringType.Name}.{property.Name}";
                var errorMessage = $"CollateAttribute cannot be used on non-string property: {name} (underlying type is {property.PrimitiveType.PrimitiveTypeKind})";
                throw new InvalidOperationException(errorMessage);
            }
        }