/// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        protected override RelationalTypeMapping GetTypeMapping(ColumnModel column)
        {
            RelationalTypeMapping mapping = null;
            if (column.DataType != null)
            {
                string underlyingDataType = null;
                column.Table.Database.SqlServer().TypeAliases
                    ?.TryGetValue(column.DataType, out underlyingDataType);

                mapping = TypeMapper.FindMapping(underlyingDataType ?? column.DataType);
            }

            return mapping;
        }
        public void Returns_unique_name_for_type()
        {
            var namer = new CSharpUniqueNamer<ColumnModel>(s => s.Name);
            var input1 = new ColumnModel
            {
                Name = "Id"
            };
            var input2 = new ColumnModel
            {
                Name = "Id"
            };

            Assert.Equal("Id", namer.GetName(input1));
            Assert.Equal("Id", namer.GetName(input1));

            Assert.Equal("Id1", namer.GetName(input2));
        }
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        protected override PropertyBuilder VisitColumn(EntityTypeBuilder builder, ColumnModel column)
        {
            var propertyBuilder = base.VisitColumn(builder, column);

            if (propertyBuilder == null)
            {
                return null;
            }

            VisitTypeMapping(propertyBuilder, column);

            VisitDefaultValue(propertyBuilder, column);

            VisitComputedValue(propertyBuilder, column);

            return propertyBuilder;
        }
 public static MySqlColumnModelAnnotations MySql(/* [NotNull] */ this ColumnModel column)
 => new MySqlColumnModelAnnotations(column);
        public SqlServerColumnModelAnnotations([NotNull] ColumnModel column)
        {
            Check.NotNull(column, nameof(column));

            _column = column;
        }
        private void GetColumns()
        {
            foreach (var table in _databaseModel.Tables)
            {
                using (var command = _connection.CreateCommand())
                {
                    command.CommandText = $"PRAGMA table_info(\"{table.Name.Replace("\"", "\"\"")}\");";

                    using (var reader = command.ExecuteReader())
                    {
                        var ordinal = 0;
                        while (reader.Read())
                        {
                            var columnName = reader.GetValueOrDefault<string>("name");
                            var dataType = reader.GetValueOrDefault<string>("type");
                            var primaryKeyOrdinal = reader.GetValueOrDefault<int>("pk");
                            var notNull = reader.GetValueOrDefault<bool>("notnull");
                            var defaultValue = reader.GetValueOrDefault<string>("dflt_value");

                            Logger.LogTrace(SqliteDesignStrings.FoundColumn(
                                table.Name, columnName, dataType, ordinal,
                                notNull, primaryKeyOrdinal, defaultValue));

                            var isPk = primaryKeyOrdinal != 0;
                            var column = new ColumnModel
                            {
                                Table = table,
                                Name = columnName,
                                DataType = dataType,
                                Ordinal = ordinal++,
                                IsNullable = !notNull && !isPk,
                                PrimaryKeyOrdinal = isPk ? primaryKeyOrdinal : default(int?),
                                DefaultValue = defaultValue
                            };

                            table.Columns.Add(column);
                            _tableColumns[ColumnKey(table, column.Name)] = column;
                        }
                    }
                }
            }
        }
        public MySqlColumnModelAnnotations(/* [NotNull] */ ColumnModel column)
        {
            // Check.NotNull(column, nameof(column));

            _column = column;
        }
Ejemplo n.º 8
0
        public NpgsqlColumnModelAnnotations([NotNull] ColumnModel column)
        {
            Check.NotNull(column, nameof(column));

            _column = column;
        }
        private void VisitComputedValue(PropertyBuilder propertyBuilder, ColumnModel column)
        {
            if (column.ComputedValue != null)
            {
                ((Property)propertyBuilder.Metadata).SetValueGenerated(null, ConfigurationSource.Explicit);
                propertyBuilder.Metadata.Relational().ComputedColumnSql = null;

                var computedExpression = ConvertSqlServerDefaultValue(column.ComputedValue);
                if (computedExpression != null)
                {
                    if (!(computedExpression == "NULL"
                          && propertyBuilder.Metadata.ClrType.IsNullableType()))
                    {
                        propertyBuilder.HasComputedColumnSql(computedExpression);
                    }
                }
                else
                {
                    Logger.LogWarning(
                        SqlServerDesignStrings.CannotInterpretComputedValue(
                            column.DisplayName,
                            column.ComputedValue,
                            propertyBuilder.Metadata.Name,
                            propertyBuilder.Metadata.DeclaringEntityType.Name));
                }
            }
        }
        private void VisitTypeMapping(PropertyBuilder propertyBuilder, ColumnModel column)
        {
            if (column.SqlServer().IsIdentity)
            {
                if (typeof(byte) == propertyBuilder.Metadata.ClrType)
                {
                    Logger.LogWarning(
                        SqlServerDesignStrings.DataTypeDoesNotAllowSqlServerIdentityStrategy(
                            column.DisplayName, column.DataType));
                }
                else
                {
                    propertyBuilder
                        .ValueGeneratedOnAdd()
                        .UseSqlServerIdentityColumn();
                }
            }

            var dateTimePrecision = column.SqlServer().DateTimePrecision;
            if (dateTimePrecision.HasValue
                && dateTimePrecision.Value != DefaultTimeTimePrecision)
            {
                propertyBuilder.Metadata.SetMaxLength(null);
                propertyBuilder.HasColumnType($"{column.DataType}({dateTimePrecision.Value})");
            }
            else if (!HasTypeAlias(column))
            {
                var qualifiedColumnTypeAndMaxLength =
                    MaxLengthQualifiedDataType(column.DataType, column.MaxLength);
                if (qualifiedColumnTypeAndMaxLength != null)
                {
                    propertyBuilder.HasColumnType(qualifiedColumnTypeAndMaxLength.Item1);
                    propertyBuilder.Metadata.SetMaxLength(qualifiedColumnTypeAndMaxLength.Item2);
                }
            }

            var columnType = propertyBuilder.Metadata.SqlServer().ColumnType;
            if (columnType != null)
            {
                TypeMapper.ValidateTypeName(columnType);
            }
        }
 private static bool HasTypeAlias(ColumnModel column)
     => column.DataType != null
        && column.Table.Database.SqlServer().TypeAliases?.ContainsKey(column.DataType) == true;
        public SqlServerColumnModelAnnotations([NotNull] ColumnModel column)
        {
            Check.NotNull(column, nameof(column));

            _column = column;
        }
        private void GetColumns()
        {
            var command = _connection.CreateCommand();
            command.CommandText = @"SELECT DISTINCT
    schema_name(t.schema_id) AS [schema],
    t.name AS [table], 
    type_name(c.user_type_id) AS [typename],
    c.name AS [column_name],
    c.column_id AS [ordinal],
    c.is_nullable AS [nullable],
    CAST(ic.key_ordinal AS int) AS [primary_key_ordinal],
    object_definition(c.default_object_id) AS [default_sql],
    cc.definition AS [computed_sql],
    CAST(CASE WHEN c.precision <> tp.precision
            THEN c.precision
            ELSE null
        END AS int) AS [precision],
    CAST(CASE WHEN c.scale <> tp.scale
            THEN c.scale
            ELSE null
        END AS int) AS [scale],
    CAST(CASE WHEN c.max_length <> tp.max_length
            THEN c.max_length
            ELSE null
        END AS int) AS [max_length],
    c.is_identity,
    c.is_computed
FROM sys.index_columns ic
    RIGHT JOIN (SELECT * FROM sys.indexes WHERE is_primary_key = 1) AS i ON i.object_id = ic.object_id AND i.index_id = ic.index_id
    RIGHT JOIN sys.columns c ON ic.object_id = c.object_id AND c.column_id = ic.column_id
    RIGHT JOIN sys.types tp ON tp.user_type_id = c.user_type_id
    LEFT JOIN sys.computed_columns cc ON cc.object_id = c.object_id AND cc.column_id = c.column_id
    JOIN sys.tables AS t ON t.object_id = c.object_id
WHERE t.name <> '" + HistoryRepository.DefaultTableName + "'" +
                                  TemporalTableWhereClause;

            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    var schemaName = reader.GetValueOrDefault<string>("schema");
                    var tableName = reader.GetValueOrDefault<string>("table");
                    var columnName = reader.GetValueOrDefault<string>("column_name");
                    var dataTypeName = reader.GetValueOrDefault<string>("typename");
                    var ordinal = reader.GetValueOrDefault<int>("ordinal");
                    var nullable = reader.GetValueOrDefault<bool>("nullable");
                    var primaryKeyOrdinal = reader.GetValueOrDefault<int?>("primary_key_ordinal");
                    var defaultValue = reader.GetValueOrDefault<string>("default_sql");
                    var computedValue = reader.GetValueOrDefault<string>("computed_sql");
                    var precision = reader.GetValueOrDefault<int?>("precision");
                    var scale = reader.GetValueOrDefault<int?>("scale");
                    var maxLength = reader.GetValueOrDefault<int?>("max_length");
                    var isIdentity = reader.GetValueOrDefault<bool>("is_identity");
                    var isComputed = reader.GetValueOrDefault<bool>("is_computed");

                    Logger.LogTrace(SqlServerDesignStrings.FoundColumn(
                        schemaName, tableName, columnName, dataTypeName, ordinal, nullable,
                        primaryKeyOrdinal, defaultValue, computedValue, precision, scale, maxLength, isIdentity, isComputed));

                    if (!_tableSelectionSet.Allows(schemaName, tableName))
                    {
                        Logger.LogTrace(
                            SqlServerDesignStrings.ColumnNotInSelectionSet(columnName, schemaName, tableName));
                        continue;
                    }

                    if (string.IsNullOrEmpty(columnName))
                    {
                        Logger.LogWarning(SqlServerDesignStrings.ColumnNameEmptyOnTable(schemaName, tableName));
                        continue;
                    }

                    TableModel table;
                    if (!_tables.TryGetValue(TableKey(tableName, schemaName), out table))
                    {
                        Logger.LogWarning(
                            SqlServerDesignStrings.UnableToFindTableForColumn(columnName, schemaName, tableName));
                        continue;
                    }

                    if (dataTypeName == "nvarchar"
                        || dataTypeName == "nchar")
                    {
                        maxLength /= 2;
                    }

                    if (dataTypeName == "decimal"
                        || dataTypeName == "numeric")
                    {
                        // maxlength here represents storage bytes. The server determines this, not the client.
                        maxLength = null;
                    }

                    var dateTimePrecision = default(int?);
                    if (_dateTimePrecisionTypes.Contains(dataTypeName))
                    {
                        dateTimePrecision = scale ?? DefaultDateTimePrecision;
                        scale = null;
                    }

                    var column = new ColumnModel
                    {
                        Table = table,
                        DataType = dataTypeName,
                        Name = columnName,
                        Ordinal = ordinal - 1,
                        IsNullable = nullable,
                        PrimaryKeyOrdinal = primaryKeyOrdinal,
                        DefaultValue = defaultValue,
                        ComputedValue = computedValue,
                        Precision = precision,
                        Scale = scale,
                        MaxLength = maxLength <= 0 ? default(int?) : maxLength,
                        ValueGenerated = isIdentity
                            ? ValueGenerated.OnAdd
                            : isComputed || dataTypeName == "timestamp"
                                ? ValueGenerated.OnAddOrUpdate
                                : default(ValueGenerated?)
                    };
                    column.SqlServer().IsIdentity = isIdentity;
                    column.SqlServer().DateTimePrecision = dateTimePrecision;

                    table.Columns.Add(column);
                    _tableColumns.Add(ColumnKey(table, column.Name), column);
                }
            }
        }
 public static SqlServerColumnModelAnnotations SqlServer([NotNull] this ColumnModel column)
 => new SqlServerColumnModelAnnotations(column);
 public static SqlCeColumnModelAnnotations SqlCe([NotNull] this ColumnModel column)
 => new SqlCeColumnModelAnnotations(column);
Ejemplo n.º 16
0
 public static NpgsqlColumnModelAnnotations Npgsql([NotNull] this ColumnModel column)
 => new NpgsqlColumnModelAnnotations(column);