Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaffolding: Generate better navigation name when FK property ends with guid #29105

Merged
merged 1 commit into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
);
}
}