Skip to content

Commit

Permalink
Capitalize database name (#27966)
Browse files Browse the repository at this point in the history
When scaffolding from a database the database name is modified in order to respect .NET conventions

#Fixes 27886
  • Loading branch information
ilmalte authored May 18, 2022
1 parent 5887391 commit 5a759d5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ public virtual string GetPrincipalEndCandidateNavigationPropertyName(
: foreignKey.DeclaringEntityType.ShortName();
}

private static string GenerateCandidateIdentifier(string originalIdentifier)
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string GenerateCandidateIdentifier(string originalIdentifier)
{
var candidateStringBuilder = new StringBuilder();
var previousLetterCharInWordIsLowerCase = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ public interface ICandidateNamingService
/// </summary>
string GenerateCandidateIdentifier(DatabaseColumn originalColumn);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
string GenerateCandidateIdentifier(string databaseName);


/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ protected virtual ModelBuilder VisitDatabaseModel(ModelBuilder modelBuilder, Dat

if (!string.IsNullOrEmpty(databaseModel.DatabaseName))
{
modelBuilder.Model.SetDatabaseName(databaseModel.DatabaseName);
modelBuilder.Model.SetDatabaseName(
!_options.UseDatabaseNames && !string.IsNullOrEmpty(databaseModel.DatabaseName)
? _candidateNamingService.GenerateCandidateIdentifier(databaseModel.DatabaseName)
: databaseModel.DatabaseName);
}

if (!string.IsNullOrEmpty(databaseModel.Collation))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public RelationalScaffoldingModelFactoryTest()
_reporter.Clear();
}

[ConditionalFact]
public void Capitalize_DatabaseName()
{
var database = new DatabaseModel { DatabaseName = "northwind" };
var model = _factory.Create(database, new ModelReverseEngineerOptions { UseDatabaseNames = false });
Assert.Equal("Northwind", model.GetDatabaseName());
}

[ConditionalFact]
public void Creates_entity_types()
{
Expand Down Expand Up @@ -131,6 +139,27 @@ public void Creates_entity_types_case_insensitive()
Assert.Equal(2, model.GetEntityTypes().Select(et => et.Name).Distinct(StringComparer.OrdinalIgnoreCase).Count());
}

[ConditionalTheory]
[InlineData("PascalCase")]
[InlineData("camelCase")]
[InlineData("snake-case")]
[InlineData("MixedCASE")]
[InlineData("separated_by_underscores")]
[InlineData("PascalCase_withUnderscore")]
[InlineData("ALL_CAPS")]
[InlineData("numbers0Dont1Affect23Upper45Case678To9LowerCase10Boundary999")]
[InlineData("We1!*~&%rdCh@r^act()0rs")]
public void Get_DatabaseName(string expectedValue)
{
var options = new ModelReverseEngineerOptions { UseDatabaseNames = true };

var database = new DatabaseModel { DatabaseName = expectedValue };
var model = _factory.Create(database, options);
Assert.Equal(expectedValue, model.GetDatabaseName());

}


[ConditionalFact]
public void Loads_column_types()
{
Expand Down

0 comments on commit 5a759d5

Please sign in to comment.