Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,13 @@ private void GetIndexes(DbConnection connection, IReadOnlyList<DatabaseTable> ta
{
using var command = connection.CreateCommand();
var commandText = @"
WITH [TablesAndViews] AS (
SELECT [object_id], [schema_id], [name], [type], [is_ms_shipped], [temporal_type]
FROM [sys].[tables]
UNION ALL
SELECT [object_id], [schema_id], [name], [type], [is_ms_shipped], 0 AS [temporal_type]
FROM [sys].[views]
)
SELECT
SCHEMA_NAME([t].[schema_id]) AS [table_schema],
[t].[name] AS [table_name],
Expand All @@ -1054,7 +1061,7 @@ private void GetIndexes(DbConnection connection, IReadOnlyList<DatabaseTable> ta
[ic].[is_descending_key],
[ic].[is_included_column]
FROM [sys].[indexes] AS [i]
JOIN [sys].[tables] AS [t] ON [i].[object_id] = [t].[object_id]
JOIN [TablesAndViews] 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 [i].[is_hypothetical] = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2012,6 +2012,74 @@ IndexProperty int
},
"DROP TABLE IndexTable;");

[ConditionalFact]
public void Create_indexes_on_views()
=> Test(
@"
CREATE TABLE dbo.BaseTable (
Id int NOT NULL,
Name int NOT NULL
);

-- Use EXEC to ensure CREATE VIEW is the start of its own batch
EXEC('
CREATE VIEW dbo.TestView
WITH SCHEMABINDING
AS
SELECT
Id,
Name,
COUNT_BIG(*) AS C
FROM dbo.BaseTable
GROUP BY Id, Name;
');

CREATE UNIQUE CLUSTERED INDEX IX_TestView_Id
ON dbo.TestView (Id);
",
[],
[],
(dbModel, scaffoldingFactory) =>
{
var view = dbModel.Tables.Single(t => t.Name == "TestView");

Assert.Single(view.Indexes);

var index = view.Indexes.Single();
Assert.True(index.IsUnique);
Assert.Collection(
index.Columns,
c => Assert.Equal("Id", c.Name));

var model = scaffoldingFactory.Create(dbModel, new ModelReverseEngineerOptions());

var viewEntity = model.GetEntityTypes()
.Single(e => e.Name == "TestView");

var properties = viewEntity.GetProperties().ToList();
Assert.Contains(properties, p => p.Name == "Id");
Assert.Contains(properties, p => p.Name == "Name");

Assert.Empty(viewEntity.GetKeys());

Assert.Collection(
viewEntity.GetIndexes(),
i =>
{
Assert.True(i.IsUnique);
Assert.Collection(
i.Properties,
p => Assert.Equal("Id", p.Name));
});

Assert.Empty(viewEntity.GetForeignKeys());
Assert.Empty(viewEntity.GetNavigations());
Assert.Empty(viewEntity.GetSkipNavigations());
},
@"
DROP VIEW dbo.TestView;
DROP TABLE dbo.BaseTable;");

[ConditionalFact]
public void Create_foreign_keys()
=> Test(
Expand Down