From 13f7a399c937044c53c78a64f394154390ecbf8e Mon Sep 17 00:00:00 2001 From: Erik Ejlskov Jensen Date: Wed, 24 Jul 2019 16:54:10 +0200 Subject: [PATCH] Exclude any indexes with hidden columns from being included in DatabaseModel (#16681) * Exclude any indexes with hidden columns. --- .../Internal/SqlServerDatabaseModelFactory.cs | 26 +++++++++++++++++-- .../SqlServerDatabaseModelFactoryTest.cs | 4 +++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/EFCore.SqlServer/Scaffolding/Internal/SqlServerDatabaseModelFactory.cs b/src/EFCore.SqlServer/Scaffolding/Internal/SqlServerDatabaseModelFactory.cs index 44eb14e0f21..52e8d4633fa 100644 --- a/src/EFCore.SqlServer/Scaffolding/Internal/SqlServerDatabaseModelFactory.cs +++ b/src/EFCore.SqlServer/Scaffolding/Internal/SqlServerDatabaseModelFactory.cs @@ -797,7 +797,7 @@ private void GetIndexes(DbConnection connection, IReadOnlyList ta { using (var command = connection.CreateCommand()) { - command.CommandText = @" + var commandText = @" SELECT SCHEMA_NAME([t].[schema_id]) AS [table_schema], [t].[name] AS [table_name], @@ -812,9 +812,31 @@ private void GetIndexes(DbConnection connection, IReadOnlyList ta FROM [sys].[indexes] AS [i] JOIN [sys].[tables] AS [t] ON [i].[object_id] = [t].[object_id] JOIN [sys].[index_columns] AS [ic] ON [i].[object_id] = [ic].[object_id] AND [i].[index_id] = [ic].[index_id] -WHERE " + tableFilter + @" +JOIN [sys].[columns] AS [c] ON [ic].[object_id] = [c].[object_id] AND [ic].[column_id] = [c].[column_id] +WHERE " + tableFilter; + + if (SupportsTemporalTable(connection)) + { + commandText += @" +AND CAST([i].[object_id] AS nvarchar(12)) + '#' + CAST([i].[index_id] AS nvarchar(12)) NOT IN +( + SELECT CAST([i].[object_id] AS nvarchar(12)) + '#' + CAST([i].[index_id] AS nvarchar(12)) + FROM [sys].[indexes] i + JOIN [sys].[tables] AS [t] ON [i].[object_id] = [t].[object_id] + JOIN [sys].[index_columns] AS [ic] ON [i].[object_id] = [ic].[object_id] AND [i].[index_id] = [ic].[index_id] + JOIN [sys].[columns] AS [c] ON [ic].[object_id] = [c].[object_id] AND [ic].[column_id] = [c].[column_id] + WHERE " + tableFilter; + + commandText += @" + AND [c].[is_hidden] = 1 +)"; + } + + commandText += @" ORDER BY [table_schema], [table_name], [index_name], [ic].[key_ordinal]"; + command.CommandText = commandText; + using (var reader = command.ExecuteReader()) { var tableIndexGroups = reader.Cast() diff --git a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/SqlServerDatabaseModelFactoryTest.cs b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/SqlServerDatabaseModelFactoryTest.cs index f34fc0bfa9e..109c01c3244 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/SqlServerDatabaseModelFactoryTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/SqlServerDatabaseModelFactoryTest.cs @@ -1528,6 +1528,9 @@ Name varchar(50) NOT NULL, PERIOD FOR SYSTEM_TIME(SysStartTime, SysEndTime) ) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.HiddenColumnsTableHistory)); +CREATE INDEX IX_HiddenColumnsTable_1 ON dbo.HiddenColumnsTable ( Name, SysStartTime); +CREATE INDEX IX_HiddenColumnsTable_2 ON dbo.HiddenColumnsTable ( SysStartTime); +CREATE INDEX IX_HiddenColumnsTable_3 ON dbo.HiddenColumnsTable ( Name ); ", Enumerable.Empty(), Enumerable.Empty(), @@ -1538,6 +1541,7 @@ PERIOD FOR SYSTEM_TIME(SysStartTime, SysEndTime) Assert.Equal(2, columns.Count); Assert.DoesNotContain(columns, c => c.Name == "SysStartTime"); Assert.DoesNotContain(columns, c => c.Name == "SysEndTime"); + Assert.Equal("IX_HiddenColumnsTable_3", dbModel.Tables.Single().Indexes.Single().Name); }, @" ALTER TABLE dbo.HiddenColumnsTable SET (SYSTEM_VERSIONING = OFF);