ErikEJ / EFCorePowerTools

Entity Framework Core Power Tools - reverse engineering, migrations and model visualization in Visual Studio & CLI
MIT License
2.18k stars 297 forks source link

Enforcement of ID column name casing - EntityID -> EntityId #627

Closed stevestokes closed 3 years ago

stevestokes commented 3 years ago

Describe what is not working as expected. Power tools is forcing the column casing in the models. In the database an identity column is set as EntityID casing and when the model renders it changes it to EntityId

Steps to reproduce

Include a complete description that I can redo to reproduce the issue. Build a model from a table with capital casing on ID

Further technical details

EF Core Power Tools version: 2.5.277.0

Database engine: SQL Server

Visual Studio version: 2019

ErikEJ commented 3 years ago

It is actually the EF Core design library that suggests this.

If you use "Use database names" and EF Core 5, it should work as expected.

stevestokes commented 3 years ago

It is actually the EF Core design library that suggests this.

If you use "Use database names" and EF Core 5, it should work as expected.

So, it sort of does. If I use "Use database names" the ID fields are preserved. However, a table name like Users is preserved as Users. In previous versions, this was singularized to "User".

So with "Use database names" ID columns = ID Users = Users db.Users.Add(...) new Users() { ... }

Without "Use database names" ID columns = Id Users = User db.User.Add(...) new User() { ... }

As you can see it flips between resulting in massive code changes either way (assuming an existing large codebase).

ErikEJ commented 3 years ago

Have you enabled Pluralization? That should fix the Users <> User issue, I think

ErikEJ commented 3 years ago

As you can see it flips between resulting in massive code changes either way (assuming an existing large codebase).

What generated that codebase?? You can opt-in to use the EF6 pluralizer...

stevestokes commented 3 years ago

image

stevestokes commented 3 years ago

If I uncheck "Use table and column names directly from the database" it results in

User { UserId }

If that is checked it results in Users { UserID }

stevestokes commented 3 years ago

DB: image

Option 1: image

results in: image

Option 2: image

results in: image

ErikEJ commented 3 years ago

Yes, those are the current options.

What is the actual issue?

stevestokes commented 3 years ago

In previous versions of this software I was able to pluralize and preserve tables names/id columns:

public partial class User
{
 public int UserID { get; set;}
}

Notice User is singular on the class and ID is not Id

ErikEJ commented 3 years ago

Which EF Core version did you pick??

ErikEJ commented 3 years ago

Please provide a sample CREATE Table script, and I will have a look.

stevestokes commented 3 years ago

Thanks!

EF Core 3.1.10

CREATE TABLE [dbo].[Users](
    [UserID] [int] IDENTITY(1,1) NOT NULL,
    [Username] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED 
(
    [UserID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ErikEJ commented 3 years ago

I wonder what "previous version of the tool" you are referring to - this has never worked until EF Core 5.

stevestokes commented 3 years ago

I recently updated it but it was about a year old, still on vs2019

ErikEJ commented 3 years ago

Ah, I used to have an quirky option to fix Id => ID. I removed that.

Please try EF Core 5 scaffolding.

ErikEJ commented 3 years ago

Any update with trying EF Core 5?

ErikEJ commented 3 years ago

As expected, this works with EF Core 5 scaffolding:

public partial class User
{
    public int UserID { get; set; }
    public string Username { get; set; }
}
MisinformedDNA commented 1 year ago

I'm running into this as well on EF Core 6. I don't want to use EF Core 5 as I'm migrating from EDMX and don't want to lose the many-to-many features. I'll probably just rename the references by hand.

ErikEJ commented 1 year ago

@MisinformedDNA are you not able to use on of the many renaming options?

MisinformedDNA commented 1 year ago

Sorry, I honestly didn't think this would work.

"ColumnPatternReplaceWith": "ID",
"ColumnRegexPattern": "ID$",
ErikEJ commented 1 year ago

@MisinformedDNA so that worked for you?

MisinformedDNA commented 1 year ago

Yep. Thanks for your hard work on this awesome project.

ErikEJ commented 1 year ago

@MisinformedDNA really appreciate your feedback and help.