private Index(IModelledIndex index, IEnumerable <IModelledColumn> includedColumns) { if (index == null) { throw new ArgumentNullException(nameof(index)); } if (includedColumns == null) { throw new ArgumentNullException(nameof(includedColumns)); } if (index.Columns == null || includedColumns.Empty() || includedColumns.AnyNull()) { throw new ArgumentException("The index has no key columns. These are required in order to declare included columns on an index.", nameof(index)); } if (index.IncludedColumns.Any()) { throw new ArgumentException("The index already has included columns on it. Do not declare an index with multiple calls to Include()", nameof(index)); } if (includedColumns == null || includedColumns.Empty() || includedColumns.AnyNull()) { throw new ArgumentNullException(nameof(includedColumns)); } Property = index.Property; Columns = index.Columns; IsUnique = index.IsUnique; IncludedColumns = includedColumns.ToList(); }
public ReflectionDatabaseTableIndex(IDatabaseDialect dialect, IRelationalDatabaseTable table, IModelledIndex index) { if (dialect == null) { throw new ArgumentNullException(nameof(dialect)); } if (table == null) { throw new ArgumentNullException(nameof(table)); } if (index == null) { throw new ArgumentNullException(nameof(index)); } IsUnique = index.IsUnique; Name = dialect.GetAliasOrDefault(index.Property !); var tableType = index.Property !.ReflectedType; var propertyLookup = tableType.GetProperties() .Select(p => new KeyValuePair <string, PropertyInfo>(p.Name, p)) .ToDictionary(); var tableColumnLookup = table.GetColumnLookup(); var columns = new List <IDatabaseIndexColumn>(); var includedColumns = new List <IDatabaseColumn>(); var isFunctionBasedIndex = false; foreach (var indexColumn in index.Columns) { if (indexColumn.Expression.IsIdentity) { var expressionName = indexColumn.Expression.DependentNames.Single().LocalName; var columnName = dialect.GetAliasOrDefault(propertyLookup[expressionName]); var tableColumn = tableColumnLookup[columnName]; var textExpression = indexColumn.Expression.ToSql(dialect); var column = new DatabaseIndexColumn(textExpression, tableColumn, indexColumn.Order); columns.Add(column); } else { isFunctionBasedIndex = true; var tableColumns = indexColumn.Expression.DependentNames .Select(name => propertyLookup.ContainsKey(name.LocalName) ? propertyLookup[name.LocalName] : null) .Where(prop => prop != null) .Select(prop => dialect.GetAliasOrDefault(prop !)) .Select(name => tableColumnLookup[name]) .ToList(); var textExpression = indexColumn.Expression.ToSql(dialect); var column = new DatabaseIndexColumn(textExpression, tableColumns, indexColumn.Order); columns.Add(column); } } foreach (var includedColumn in index.IncludedColumns) { var includedColumnName = dialect.GetAliasOrDefault(includedColumn.Property !); var column = tableColumnLookup[includedColumnName]; includedColumns.Add(column); } IsFunctionBased = isFunctionBasedIndex; Columns = columns; IncludedColumns = includedColumns; }