question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Database-first scaffolding generates entity names ignoring original casing

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ghostcommented, Jan 10, 2018

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 in CandidateNamingService is right. It’ll still produce names (when the above flag is not used) like

DOB >> Dob

0reactions
ajcvickerscommented, Jan 12, 2018

Closing 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Database-first scaffolding generates entity names ignoring ...
This I've fixed using the IPluralizer interface. BUT, the final entity classes created by EF Core , have names that ignores the original...
Read more >
How to customise entity name casing in EF Core with ...
BUT, in the final entity class created by EF Core , the second property name becomes Xmldetail ignoring the original casing!
Read more >
Scaffolding (Reverse Engineering) - EF Core
Reverse engineering is the process of scaffolding entity type classes and a DbContext class based on a database schema.
Read more >
Generating a model from an existing database
The following example illustrates how to use code first to generate a model from a SQL Server database in a new console application...
Read more >
Using Entity Framework With an Existing Database
In this case the answer is to change the code that is generated during the creation of the classes and DbContext . Note:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found