diff --git a/src/Core/RevEng.Core.60/PatchedSqlServerDatabaseModelFactory.cs b/src/Core/RevEng.Core.60/PatchedSqlServerDatabaseModelFactory.cs index 92a73337b..d63a6e3ea 100644 --- a/src/Core/RevEng.Core.60/PatchedSqlServerDatabaseModelFactory.cs +++ b/src/Core/RevEng.Core.60/PatchedSqlServerDatabaseModelFactory.cs @@ -22,6 +22,7 @@ using Microsoft.EntityFrameworkCore.SqlServer.Metadata.Internal; using Microsoft.EntityFrameworkCore.SqlServer.Scaffolding.Internal; using Microsoft.EntityFrameworkCore.Utilities; +using Microsoft.Extensions.Logging; #nullable enable @@ -635,10 +636,7 @@ FROM [sys].[views] AS [v] // This is done separately due to MARS property may be turned off GetColumns(connection, tables, filter, viewFilter, typeAliases, databaseCollation); - if (SupportsIndexes()) - { - GetIndexes(connection, tables, filter); - } + GetIndexes(connection, tables, filter); GetForeignKeys(connection, tables, filter); @@ -929,7 +927,7 @@ private void GetIndexes(DbConnection connection, IReadOnlyList ta [i].[has_filter], [i].[filter_definition], [i].[fill_factor], - COL_NAME([ic].[object_id], [ic].[column_id]) AS [column_name], + [c].[name] AS [column_name], [ic].[is_included_column] FROM [sys].[indexes] AS [i] JOIN [sys].[tables] AS [t] ON [i].[object_id] = [t].[object_id] @@ -986,7 +984,8 @@ FROM [sys].[indexes] i if (primaryKeyGroups.Length == 1) { - if (TryGetPrimaryKey(primaryKeyGroups[0], out var primaryKey)) + if (TryGetPrimaryKey(primaryKeyGroups[0], out var primaryKey) + && IsValidPrimaryKey(primaryKey)) { _logger.PrimaryKeyFound(primaryKey.Name!, DisplayName(tableSchema, tableName)); table.PrimaryKey = primaryKey; @@ -1062,6 +1061,14 @@ bool TryGetPrimaryKey( return true; } + bool IsValidPrimaryKey(DatabasePrimaryKey primaryKey) + { + if (_engineEdition != 1000) + return true; + + return primaryKey.Columns.Count == 1 && primaryKey.Columns[0].StoreType == "uniqueidentifier"; + } + bool TryGetUniqueConstraint( IGrouping<(string? Name, string? TypeDesc), DbDataRecord> uniqueConstraintGroup, [NotNullWhen(true)] out DatabaseUniqueConstraint? uniqueConstraint) @@ -1260,6 +1267,14 @@ FROM [sys].[foreign_keys] AS [f] foreignKey.PrincipalColumns.Add(principalColumn); } + if (!invalid && _engineEdition == 1000 && !IsValidDataverseForeignKey(foreignKey)) + { + invalid = true; + _logger.Logger.LogWarning("ForeignKey {ForeignKeyName} on table {TableName} is not supported in Dataverse.", + fkName!, + DisplayName(table.Schema, table.Name)); + } + if (!invalid) { if (foreignKey.Columns.SequenceEqual(foreignKey.PrincipalColumns)) @@ -1287,6 +1302,26 @@ FROM [sys].[foreign_keys] AS [f] table.ForeignKeys.Add(foreignKey); } } + + bool IsValidDataverseForeignKey(DatabaseForeignKey foreignKey) + { + if (foreignKey.Columns.Count != 1) + return false; + + if (foreignKey.Columns[0].StoreType != "uniqueidentifier") + return false; + + if (foreignKey.PrincipalTable.PrimaryKey == null) + return false; + + if (foreignKey.PrincipalTable.PrimaryKey.Columns.Count != 1) + return false; + + if (foreignKey.PrincipalTable.PrimaryKey.Columns[0].Name != foreignKey.PrincipalColumns[0].Name) + return false; + + return true; + } } } } @@ -1300,9 +1335,6 @@ private bool SupportsMemoryOptimizedTable() private bool SupportsSequences() => _compatibilityLevel >= 110 && (_engineEdition != 6 && _engineEdition != 11 && _engineEdition != 1000); - private bool SupportsIndexes() - => _engineEdition != 1000; - private bool SupportsViews() => _engineEdition != 1000; diff --git a/src/Core/RevEng.Core.80/RevEng.Core.80.csproj b/src/Core/RevEng.Core.80/RevEng.Core.80.csproj index 63180a992..ffc2de70b 100644 --- a/src/Core/RevEng.Core.80/RevEng.Core.80.csproj +++ b/src/Core/RevEng.Core.80/RevEng.Core.80.csproj @@ -21,7 +21,6 @@ - diff --git a/src/Core/RevEng.Core.90/RevEng.Core.90.csproj b/src/Core/RevEng.Core.90/RevEng.Core.90.csproj index c342dec0e..1bb888ec2 100644 --- a/src/Core/RevEng.Core.90/RevEng.Core.90.csproj +++ b/src/Core/RevEng.Core.90/RevEng.Core.90.csproj @@ -21,7 +21,6 @@ -