-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Allow using foreign keys in Dataverse reverse engineering #34689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MarkMpn
wants to merge
2
commits into
dotnet:main
Choose a base branch
from
MarkMpn:dataverse-foreign-keys
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/EFCore.SqlServer/Properties/SqlServerStrings.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -682,10 +682,7 @@ FROM [sys].[views] AS [v] | |
// This is done separately due to MARS property may be turned off | ||
GetColumns(connection, tables, tableFilterSql, viewFilter, typeAliases, databaseCollation); | ||
|
||
if (SupportsIndexes()) | ||
{ | ||
GetIndexes(connection, tables, tableFilterSql); | ||
} | ||
GetIndexes(connection, tables, tableFilterSql); | ||
|
||
GetForeignKeys(connection, tables, tableFilterSql); | ||
|
||
|
@@ -1028,7 +1025,7 @@ private void GetIndexes(DbConnection connection, IReadOnlyList<DatabaseTable> 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_descending_key], | ||
[ic].[is_included_column] | ||
FROM [sys].[indexes] AS [i] | ||
|
@@ -1089,7 +1086,7 @@ 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; | ||
|
@@ -1137,6 +1134,16 @@ FROM [sys].[indexes] i | |
} | ||
} | ||
|
||
bool IsValidPrimaryKey(DatabasePrimaryKey primaryKey) | ||
{ | ||
if (_engineEdition != EngineEdition.DynamicsTdsEndpoint) | ||
{ | ||
return true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use brackets, not one line return statements |
||
} | ||
|
||
return primaryKey.Columns.Count == 1 && primaryKey.Columns[0].StoreType == "uniqueidentifier"; | ||
} | ||
|
||
bool TryGetPrimaryKey( | ||
IGrouping<(string Name, string? TypeDesc, byte FillFactor), DbDataRecord> primaryKeyGroup, | ||
[NotNullWhen(true)] out DatabasePrimaryKey? primaryKey) | ||
|
@@ -1377,6 +1384,14 @@ FROM [sys].[foreign_keys] AS [f] | |
foreignKey.PrincipalColumns.Add(principalColumn); | ||
} | ||
|
||
if (!invalid && _engineEdition == EngineEdition.DynamicsTdsEndpoint && !IsValidDataverseForeignKey(foreignKey)) | ||
{ | ||
invalid = true; | ||
_logger.DataverseForeignKeyInvalidWarning( | ||
fkName!, | ||
DisplayName(table.Schema, table.Name)); | ||
} | ||
|
||
if (!invalid) | ||
{ | ||
if (foreignKey.Columns.SequenceEqual(foreignKey.PrincipalColumns)) | ||
|
@@ -1406,6 +1421,36 @@ FROM [sys].[foreign_keys] AS [f] | |
} | ||
} | ||
} | ||
|
||
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; | ||
} | ||
} | ||
|
||
private void GetTriggers(DbConnection connection, IReadOnlyList<DatabaseTable> tables, string tableFilter) | ||
|
@@ -1456,9 +1501,6 @@ private bool SupportsMemoryOptimizedTable() | |
private bool SupportsSequences() | ||
=> _compatibilityLevel >= 110 && IsFullFeaturedEngineEdition(); | ||
|
||
private bool SupportsIndexes() | ||
=> _engineEdition != EngineEdition.DynamicsTdsEndpoint; | ||
|
||
private bool SupportsViews() | ||
=> _engineEdition != EngineEdition.DynamicsTdsEndpoint; | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wonder why COL_NAME was used previously??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was my main concern, I haven't found a situation yet where this produces different results but I assume it must be there for some reason. It was introduced in 746ff59 but I can't see any specific reasoning behind it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah! It was there because earlier there was no join with sys.columns - and it was just left there
https://github.com/dotnet/efcore/blob/32434d2d86a75e091e331940384115e447ff33de/src/EFCore.SqlServer/Scaffolding/Internal/SqlServerDatabaseModelFactory.cs - so nothing to be concerned about