Skip to content

Commit

Permalink
RevEng: Consider skip navigation names when generating identifier for…
Browse files Browse the repository at this point in the history
… members of class (#26531)

Resolves #26496

We had mechanism to generate unique identifiers to assign to skip navigations but the code to find existing identifiers lacked skip navigations. So skip navigation names were not unique-fied throwing error when trying to add skip navigation with same name
  • Loading branch information
smitpatel authored Nov 9, 2021
1 parent 22d1a09 commit 62dc53f
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,11 @@ protected virtual List<string> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});

}
}
}

0 comments on commit 62dc53f

Please sign in to comment.