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 6f63543
Show file tree
Hide file tree
Showing 2 changed files with 156 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,152 @@ 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);
}
);
}

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

blogTable.UniqueConstraints.Add(
new DatabaseUniqueConstraint
{
Table = blogTable,
Name = "AK_Foo",
Columns = { blogTable.Columns.ElementAt(1), blogTable.Columns.ElementAt(2) }
});

postTable.ForeignKeys.Add(
new DatabaseForeignKey
{
Table = postTable,
Name = "FK_Foo",
Columns = { postTable.Columns.ElementAt(1), postTable.Columns.ElementAt(2) },
PrincipalTable = blogTable,
PrincipalColumns = { blogTable.Columns.ElementAt(1), blogTable.Columns.ElementAt(2) },
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 6f63543

Please sign in to comment.