diff --git a/src/EFCore.Design/Scaffolding/Internal/RelationalScaffoldingModelFactory.cs b/src/EFCore.Design/Scaffolding/Internal/RelationalScaffoldingModelFactory.cs index 46418cb52a4..0372e7b8d18 100644 --- a/src/EFCore.Design/Scaffolding/Internal/RelationalScaffoldingModelFactory.cs +++ b/src/EFCore.Design/Scaffolding/Internal/RelationalScaffoldingModelFactory.cs @@ -1003,6 +1003,11 @@ protected virtual List ExistingIdentifiers(IReadOnlyEntityType entityTyp } existingIdentifiers.AddRange(entityType.GetNavigations().Select(p => p.Name)); + if (!(AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue26496", out var enabled) + && enabled)) + { + existingIdentifiers.AddRange(entityType.GetSkipNavigations().Select(p => p.Name)); + } return existingIdentifiers; } diff --git a/test/EFCore.Design.Tests/Scaffolding/Internal/RelationalScaffoldingModelFactoryTest.cs b/test/EFCore.Design.Tests/Scaffolding/Internal/RelationalScaffoldingModelFactoryTest.cs index c2a84daa5a8..04f985b6628 100644 --- a/test/EFCore.Design.Tests/Scaffolding/Internal/RelationalScaffoldingModelFactoryTest.cs +++ b/test/EFCore.Design.Tests/Scaffolding/Internal/RelationalScaffoldingModelFactoryTest.cs @@ -2256,5 +2256,122 @@ public void Scaffold_skip_navigation_for_many_to_many_join_table_basic() }); } + + [ConditionalFact] + public void Scaffold_skip_navigation_for_many_to_many_join_table_self_ref() + { + var database = new DatabaseModel + { + Tables = + { + new DatabaseTable + { + Name = "Products", + Columns = + { + new DatabaseColumn + { + Name = "Id", + StoreType = "int" + } + }, + PrimaryKey = new DatabasePrimaryKey { Columns = { new DatabaseColumnRef("Id") } } + }, + new DatabaseTable + { + Name = "RelatedProducts", + Columns = + { + new DatabaseColumn + { + Name = "Id", + StoreType = "int" + }, + new DatabaseColumn + { + Name = "ProductId", + StoreType = "int" + } + }, + PrimaryKey = new DatabasePrimaryKey { + Columns = { new DatabaseColumnRef("Id"), new DatabaseColumnRef("ProductId") } }, + ForeignKeys = + { + new DatabaseForeignKey + { + Columns = { new DatabaseColumnRef("Id") }, + PrincipalColumns = { new DatabaseColumnRef("Id") }, + PrincipalTable = new DatabaseTableRef("Products"), + }, + new DatabaseForeignKey + { + Columns = { new DatabaseColumnRef("ProductId") }, + PrincipalColumns = { new DatabaseColumnRef("Id") }, + PrincipalTable = new DatabaseTableRef("Products"), + } + } + }, + new DatabaseTable + { + Name = "SubProducts", + Columns = + { + new DatabaseColumn + { + Name = "Id", + StoreType = "int" + }, + new DatabaseColumn + { + Name = "ProductId", + StoreType = "int" + } + }, + PrimaryKey = new DatabasePrimaryKey { + Columns = { new DatabaseColumnRef("Id"), new DatabaseColumnRef("ProductId") } }, + ForeignKeys = + { + new DatabaseForeignKey + { + Columns = { new DatabaseColumnRef("Id") }, + PrincipalColumns = { new DatabaseColumnRef("Id") }, + PrincipalTable = new DatabaseTableRef("Products"), + }, + new DatabaseForeignKey + { + Columns = { new DatabaseColumnRef("ProductId") }, + PrincipalColumns = { new DatabaseColumnRef("Id") }, + PrincipalTable = new DatabaseTableRef("Products"), + } + } + } + } + }; + + var model = _factory.Create(database, new ModelReverseEngineerOptions()); + + Assert.Collection( + model.GetEntityTypes().OrderBy(e => e.Name), + t1 => + { + Assert.Empty(t1.GetNavigations()); + Assert.Collection(t1.GetSkipNavigations(), + s => Assert.Equal("Ids", s.Name), + s => Assert.Equal("IdsNavigation", s.Name), + s => Assert.Equal("Products", s.Name), + s => Assert.Equal("ProductsNavigation", s.Name)); + }, + t2 => + { + Assert.Empty(t2.GetNavigations()); + Assert.Equal(2, t2.GetForeignKeys().Count()); + }, + t2 => + { + Assert.Empty(t2.GetNavigations()); + Assert.Equal(2, t2.GetForeignKeys().Count()); + }); + + } } }