diff --git a/src/EFCore.SqlServer/Scaffolding/Internal/SqlServerDatabaseModelFactory.cs b/src/EFCore.SqlServer/Scaffolding/Internal/SqlServerDatabaseModelFactory.cs index a7e4ca29d43..2e875d28035 100644 --- a/src/EFCore.SqlServer/Scaffolding/Internal/SqlServerDatabaseModelFactory.cs +++ b/src/EFCore.SqlServer/Scaffolding/Internal/SqlServerDatabaseModelFactory.cs @@ -1378,6 +1378,7 @@ FROM [sys].[foreign_keys] AS [f] var duplicated = table.ForeignKeys .FirstOrDefault( k => k.Columns.SequenceEqual(foreignKey.Columns) + && k.PrincipalColumns.SequenceEqual(foreignKey.PrincipalColumns) && k.PrincipalTable.Equals(foreignKey.PrincipalTable)); if (duplicated != null) { diff --git a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/SqlServerDatabaseModelFactoryTest.cs b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/SqlServerDatabaseModelFactoryTest.cs index 9864bb0d71f..2b72eed9f3b 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/SqlServerDatabaseModelFactoryTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/SqlServerDatabaseModelFactoryTest.cs @@ -2893,6 +2893,10 @@ public void Skip_duplicate_foreign_key() => Test( @"CREATE TABLE PrincipalTable ( Id int PRIMARY KEY, + Value1 uniqueidentifier, + Value2 uniqueidentifier, + CONSTRAINT [UNIQUE_Value1] UNIQUE ([Value1] ASC), + CONSTRAINT [UNIQUE_Value2] UNIQUE ([Value2] ASC), ); CREATE TABLE OtherPrincipalTable ( @@ -2902,9 +2906,12 @@ CREATE TABLE OtherPrincipalTable ( CREATE TABLE DependentTable ( Id int PRIMARY KEY, ForeignKeyId int, + ValueKey uniqueidentifier, CONSTRAINT MYFK1 FOREIGN KEY (ForeignKeyId) REFERENCES PrincipalTable(Id), CONSTRAINT MYFK2 FOREIGN KEY (ForeignKeyId) REFERENCES PrincipalTable(Id), CONSTRAINT MYFK3 FOREIGN KEY (ForeignKeyId) REFERENCES OtherPrincipalTable(Id), + CONSTRAINT MYFK4 FOREIGN KEY (ValueKey) REFERENCES PrincipalTable(Value1), + CONSTRAINT MYFK5 FOREIGN KEY (ValueKey) REFERENCES PrincipalTable(Value2), );", Enumerable.Empty(), Enumerable.Empty(), @@ -2919,7 +2926,7 @@ CONSTRAINT MYFK3 FOREIGN KEY (ForeignKeyId) REFERENCES OtherPrincipalTable(Id), Assert.Equal(LogLevel.Warning, level); var table = dbModel.Tables.Single(t => t.Name == "DependentTable"); - Assert.Equal(2, table.ForeignKeys.Count); + Assert.Equal(4, table.ForeignKeys.Count); }, @" DROP TABLE DependentTable;