private PropertyBuilder VisitDefaultValue(ColumnModel column, PropertyBuilder propertyBuilder)
        {
            if (column.DefaultValue != null)
            {
                // unset default
                propertyBuilder.Metadata.ValueGenerated = null;
                propertyBuilder.Metadata.Relational().GeneratedValueSql = null;

                var property = propertyBuilder.Metadata;
                var defaultExpressionOrValue =
                    _sqlServerLiteralUtilities
                    .ConvertSqlServerDefaultValue(
                        property.ClrType, column.DefaultValue);
                if (defaultExpressionOrValue?.DefaultExpression != null)
                {
                    propertyBuilder.HasDefaultValueSql(defaultExpressionOrValue.DefaultExpression);
                }
                else if (defaultExpressionOrValue != null)
                {
                    // Note: defaultExpressionOrValue.DefaultValue == null is valid
                    propertyBuilder.HasDefaultValue(defaultExpressionOrValue.DefaultValue);
                }
                else
                {
                    Logger.LogWarning(
                        SqlServerDesignStrings.UnableToConvertDefaultValue(
                            column.DisplayName, column.DefaultValue,
                            property.ClrType, property.Name, property.DeclaringEntityType.Name));
                }
            }
            return(propertyBuilder);
        }
Пример #2
0
        public virtual void ApplyPropertyProperties(
            [NotNull] Property property, [NotNull] TableColumn tableColumn)
        {
            Check.NotNull(property, nameof(property));
            Check.NotNull(tableColumn, nameof(tableColumn));

            property.IsNullable = tableColumn.IsNullable;

            if (property.Name != tableColumn.ColumnName)
            {
                property.Relational().Column = tableColumn.ColumnName;
            }

            string typeName = null;

            if (DataTypesForNumericPrecisionAndScale.Contains(tableColumn.DataType))
            {
                if (tableColumn.NumericPrecision.HasValue)
                {
                    if (tableColumn.Scale.HasValue)
                    {
                        typeName = tableColumn.DataType + "(" + tableColumn.NumericPrecision.Value + ", " + tableColumn.Scale.Value + ")";
                    }
                    else
                    {
                        typeName = tableColumn.DataType + "(" + tableColumn.NumericPrecision.Value + ")";
                    }
                }
            }
            else if (DataTypesForDateTimePrecisionAndScale.Contains(tableColumn.DataType))
            {
                if (tableColumn.DateTimePrecision.HasValue)
                {
                    if (tableColumn.Scale.HasValue)
                    {
                        typeName = tableColumn.DataType + "(" + tableColumn.DateTimePrecision.Value + ", " + tableColumn.Scale.Value + ")";
                    }
                    else
                    {
                        typeName = tableColumn.DataType + "(" + tableColumn.DateTimePrecision.Value + ")";
                    }
                }
            }

            if (typeName != null)
            {
                property.Relational().ColumnType = typeName;
            }

            if (tableColumn.IsIdentity)
            {
                if (typeof(byte) == SqlServerTypeMapping._sqlTypeToClrTypeMap[tableColumn.DataType])
                {
                    _logger.LogWarning(
                        Strings.DataTypeDoesNotAllowIdentityStrategy(tableColumn.Id, tableColumn.DataType));
                }
                else
                {
                    property.SqlServer().IdentityStrategy = SqlServerIdentityStrategy.IdentityColumn;
                }
            }

            if (tableColumn.IsStoreGenerated ||
                tableColumn.DataType == "timestamp")
            {
                // timestamp columns should always be treated as store generated
                property.ValueGenerated = ValueGenerated.OnAddOrUpdate;
            }

            if (tableColumn.DefaultValue != null)
            {
                var defaultExpressionOrValue =
                    _sqlServerLiteralUtilities
                    .ConvertSqlServerDefaultValue(
                        property.ClrType, tableColumn.DefaultValue);
                if (defaultExpressionOrValue != null &&
                    defaultExpressionOrValue.DefaultExpression != null)
                {
                    property.Relational().GeneratedValueSql = defaultExpressionOrValue.DefaultExpression;
                }
                else if (defaultExpressionOrValue != null &&
                         defaultExpressionOrValue.DefaultValue != null)
                {
                    property.Relational().DefaultValue = defaultExpressionOrValue.DefaultValue;
                }
                else
                {
                    _logger.LogWarning(
                        Strings.UnableToConvertDefaultValue(
                            tableColumn.Id, tableColumn.DefaultValue,
                            property.ClrType, property.Name, property.DeclaringEntityType.Name));
                }
            }
        }