Skip to content

Commit

Permalink
Scaffolding: Generate better navigation name when FK property ends wi…
Browse files Browse the repository at this point in the history
…th guid

Resolves #26990
  • Loading branch information
smitpatel committed Sep 15, 2022
1 parent dfbc7ff commit f1cb4d1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,15 @@ private static string StripId(string commonPrefix)
return commonPrefix;
}

var ignoredCharacterCount = 2;
if (commonPrefix.Length > 4
&& commonPrefix.EndsWith("guid", StringComparison.OrdinalIgnoreCase))
{
ignoredCharacterCount = 4;
}

int i;
for (i = commonPrefix.Length - 3; i >= 0; i--)
for (i = commonPrefix.Length - ignoredCharacterCount - 1; i >= 0; i--)
{
if (char.IsLetterOrDigit(commonPrefix[i]))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2452,4 +2452,62 @@ public void Scaffold_skip_navigation_for_many_to_many_join_table_self_ref()
Assert.Equal(2, t2.GetForeignKeys().Count());
});
}

[ConditionalFact]
public void Fk_property_ending_in_guid_navigation_name()
{
var blogTable = new DatabaseTable
{
Database = Database,
Name = "Blog",
Columns = { IdColumn },
PrimaryKey = IdPrimaryKey
};
var postTable = new DatabaseTable
{
Database = Database,
Name = "Post",
Columns =
{
IdColumn,
new DatabaseColumn
{
Table = Table,
Name = "BlogGuid",
StoreType = "int",
IsNullable = true
}
},
PrimaryKey = IdPrimaryKey
};

postTable.ForeignKeys.Add(
new DatabaseForeignKey
{
Table = postTable,
Name = "FK_Foo",
Columns = { postTable.Columns.ElementAt(1) },
PrincipalTable = blogTable,
PrincipalColumns = { blogTable.Columns.ElementAt(0) },
OnDelete = ReferentialAction.Cascade
});

var info = new DatabaseModel { Tables = { blogTable, postTable } };

var model = _factory.Create(info, new ModelReverseEngineerOptions());

Assert.Collection(
model.GetEntityTypes().OrderBy(t => t.Name).Cast<EntityType>(),
entity =>
{
Assert.Equal("Blog", entity.Name);
Assert.Equal("Posts", entity.GetNavigations().Single().Name);
},
entity =>
{
Assert.Equal("Post", entity.Name);
Assert.Equal("Blog", entity.GetNavigations().Single().Name);
}
);
}
}

0 comments on commit f1cb4d1

Please sign in to comment.