Skip to content
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

RevEng: Warn for FK with same required facets exists and skip it #23484

Merged
1 commit merged into from
Nov 25, 2020
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
12 changes: 10 additions & 2 deletions src/EFCore.Design/Properties/DesignStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/EFCore.Design/Properties/DesignStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@
<data name="ForeignKeyScaffoldErrorPropertyNotFound" xml:space="preserve">
<value>Could not scaffold the foreign key '{foreignKeyName}'. The following columns in the foreign key could not be scaffolded: {columnNames}.</value>
</data>
<data name="ForeignKeyWithSameFacetsExists" xml:space="preserve">
<value>Could not scaffold the foreign key '{foreignKeyName}'. Foreign key '{existingForeignKey}' is defined on same columns targeting same key on principal table.</value>
</data>
<data name="ForeignMigrations" xml:space="preserve">
<value>The namespace '{migrationsNamespace}' contains migrations for a different DbContext. This can result in conflicting migration names. It's recommend to put migrations for different DbContext classes into different namespaces.</value>
</data>
Expand Down Expand Up @@ -369,4 +372,4 @@ Change your target project to the migrations project by using the Package Manage
<data name="WritingSnapshot" xml:space="preserve">
<value>Writing model snapshot to '{file}'.</value>
</data>
</root>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,6 @@ protected virtual IMutableForeignKey VisitForeignKey([NotNull] ModelBuilder mode
}

var dependentEntityType = modelBuilder.Model.FindEntityType(GetEntityTypeName(foreignKey.Table));

if (dependentEntityType == null)
{
return null;
Expand Down Expand Up @@ -824,9 +823,7 @@ protected virtual IMutableForeignKey VisitForeignKey([NotNull] ModelBuilder mode
nullablePrincipalProperties.Select(tuple => tuple.column.DisplayName()).ToList()
.Aggregate((a, b) => a + "," + b)));

nullablePrincipalProperties
.ToList()
.ForEach(tuple => tuple.property.IsNullable = false);
nullablePrincipalProperties.ForEach(tuple => tuple.property.IsNullable = false);
}

principalKey = principalEntityType.AddKey(principalProperties);
Expand All @@ -845,6 +842,15 @@ protected virtual IMutableForeignKey VisitForeignKey([NotNull] ModelBuilder mode
}
}

var existingForeignKey = dependentEntityType.FindForeignKey(dependentProperties, principalKey, principalEntityType);
if (existingForeignKey is not null)
{
_reporter.WriteWarning(
DesignStrings.ForeignKeyWithSameFacetsExists(foreignKey.DisplayName(), existingForeignKey.GetConstraintName()));

return null;
}

var newForeignKey = dependentEntityType.AddForeignKey(
dependentProperties, principalKey, principalEntityType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,64 @@ public void It_logs_warning_for_bad_foreign_key()
childrenTable.ForeignKeys.ElementAt(0).DisplayName(), "NotPkId", "Parent")));
}

[ConditionalFact]
public void It_logs_warning_for_duplicate_foreign_key()
{
var parentTable = new DatabaseTable
{
Database = Database,
Name = "Parent",
Columns =
{
IdColumn
},
PrimaryKey = IdPrimaryKey
};
var childrenTable = new DatabaseTable
{
Database = Database,
Name = "Children",
Columns =
{
IdColumn,
new DatabaseColumn
{
Table = Table,
Name = "ParentId",
StoreType = "int"
}
},
PrimaryKey = IdPrimaryKey
};
childrenTable.ForeignKeys.Add(
new DatabaseForeignKey
{
Table = childrenTable,
Name = "FK_Foo",
Columns = { childrenTable.Columns.ElementAt(1) },
PrincipalTable = parentTable,
PrincipalColumns = { parentTable.Columns.ElementAt(0) }
});
childrenTable.ForeignKeys.Add(
new DatabaseForeignKey
{
Table = childrenTable,
Name = "FK_Another_Foo",
Columns = { childrenTable.Columns.ElementAt(1) },
PrincipalTable = parentTable,
PrincipalColumns = { parentTable.Columns.ElementAt(0) }
});

_factory.Create(
new DatabaseModel { Tables = { parentTable, childrenTable } },
new ModelReverseEngineerOptions());

Assert.Single(
_reporter.Messages, t => t.Contains(
"warn: "
+ DesignStrings.ForeignKeyWithSameFacetsExists(childrenTable.ForeignKeys.ElementAt(1).DisplayName(), "FK_Foo")));
}

[ConditionalFact]
public void Unique_nullable_index_unused_by_foreign_key()
{
Expand Down