Database-first scaffolding generates entity names ignoring original casing
See original GitHub issueI have an existing EF6
code base, and I’m trying to port that to EF Core
. I’m using the following command to scaffold the entity classes from my database
Scaffold-DbContext "MyConnectionString" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
It does generate the DbContext
and the necessary entities. But things have changed a lot from EF6
including the naming conventions - how to name entities, properties & navigation (the newly created entities are very different from what I had with my EF6 code).
One of the changes is - Now EF does not singularize class names from table name. e.g. if table name is ‘Accessories’, earlier entity class used to be ‘Accessory’, now it is ‘Accessories’! This I’ve fixed using the IPluralizer interface.
BUT, the final entity classes created by EF Core
, have names that ignores the original casing! See the following Table-Name
that I have in my existing database & the Entity-Name
that is generated by EF.
Students >> Students StudentScores >> StudentScores XMLDetails >> Xmldetails (this is the problem)
This is different form EF6
behaviour and it also reduces readability of code and developer’s productivity (apart from the fact, I’m having to manually change my existing code all over which I’m trying to port).
There are related issues 9257 and 9477 but they deal with the use case - when there are two tables in DB2 with same name but different casing.
The problem seems to come from the way EF code does PascalCase-ing of the names
//namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal
//class CandidateNamingService
//method GenerateCandidateIdentifier(string originalIdentifier)
...isFirstCharacterInWord & other logic
candidateStringBuilder.Append(isFirstCharacterInWord ? char.ToUpperInvariant(c) : char.ToLowerInvariant(c));
Further technical details
EF Core version: 2.0.1 Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: Windows 8.1 IDE: Visual Studio Community 2017 v15.5.2
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:5 (3 by maintainers)
Found the other issue and the fix. This previous issue is #9820 - UseDatabaseNames not working in columns names. It was fixed in #9981.
With the fix, the names should match correctly with database when
--use-database-names
flag is used. BUT I still doubt if way of PascalCase-ing inCandidateNamingService
is right. It’ll still produce names (when the above flag is not used) likeClosing this as duplicate. @chakrabar Changing “DOB” to “Dob” is by-design. It’s a pretty common code style to do this, and, for what it’s worth, it’s even in the Microsoft design guidelines.