Arghya C
Arghya C

Reputation: 10068

How to customise entity name casing in EF Core with database first

I have an existing EF6 code base, and I'm trying to port that to EF Core, and I have an existing database which I cannot erase/modify. So 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. Now, seems like they have changed the naming conventions (how to name entities, properties & navigation) A LOT and the newly created entities are very different from my existing entities! I couldn't find a way to customise these conventions, so I'm having to manually change the code in many places! (sad!)

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'!

To fix this, I have used the IPluralizer interface and used the Humanizer library to Singularize entity class names.

When I use the Humanizer library, it does this

"Accessories".Singularize(); //produces "Accessory"
"XMLDetails".Singularize(); //produces "XMLDetail"

BUT, in the final entity class created by EF Core, the second property name becomes Xmldetail ignoring the original casing! Is there a way to retain the original casing through the process of EF Core scaffolding?

Note: I'm using Microsoft.EntityFrameworkCore.SqlServer version 2.0.1 with a class library targeting .NET Standard 2.0.

Upvotes: 4

Views: 6555

Answers (2)

Arghya C
Arghya C

Reputation: 10068

After a bit of research I came to know from the GitHub community that, this is a known issue in EF Core 2.0 and it has already been fixed in the repo. The fix is scheduled to release with EF Core 2.1.

Once the fix is released you can use the --use-database-names flag to tell EF to generate names exactly according to the database names like this

Scaffold-DbContext "ConnString" Microsoft.EntityFrameworkCore.SqlServer -o Models --use-database-names

Until that, you can try one of the following tools which gives more/better options than default EF Core tools (I have not practically used them yet)

  1. EF Core Power Tools
  2. EntityFramework-Reverse-POCO-Code-First-Generator

Upvotes: 3

David Hruška
David Hruška

Reputation: 168

I am not sure about this.

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'!

What I know so far, there is an option when connection EF to DB. It does the same output as earlier versions.

Like this: https://dotnetinterviewquestion.files.wordpress.com/2013/10/ut.png

Upvotes: 0

Related Questions